home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_glib.idb / usr / freeware / include / glib-1.2 / glib.h.z / glib.h
C/C++ Source or Header  |  2001-04-12  |  92KB  |  2,828 lines

  1. /* GLIB - Library of useful routines for C programming
  2.  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Library General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.
  8.  *
  9.  * This library is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
  12.  * Library General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Library General Public
  15.  * License along with this library; if not, write to the
  16.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.  * Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. /*
  21.  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
  22.  * file for a list of people on the GLib Team.  See the ChangeLog
  23.  * files for a list of changes.  These files are distributed with
  24.  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
  25.  */
  26.  
  27. #ifndef __G_LIB_H__
  28. #define __G_LIB_H__
  29.  
  30. /* system specific config file glibconfig.h provides definitions for
  31.  * the extrema of many of the standard types. These are:
  32.  *
  33.  *  G_MINSHORT, G_MAXSHORT
  34.  *  G_MININT, G_MAXINT
  35.  *  G_MINLONG, G_MAXLONG
  36.  *  G_MINFLOAT, G_MAXFLOAT
  37.  *  G_MINDOUBLE, G_MAXDOUBLE
  38.  *
  39.  * It also provides the following typedefs:
  40.  *
  41.  *  gint8, guint8
  42.  *  gint16, guint16
  43.  *  gint32, guint32
  44.  *  gint64, guint64
  45.  *
  46.  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
  47.  * this file). 
  48.  *
  49.  * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
  50.  * This is useful to pass an integer instead of a pointer to a callback.
  51.  *
  52.  *  GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
  53.  *  GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
  54.  *
  55.  * Finally, it provide the following wrappers to STDC functions:
  56.  *
  57.  *  g_ATEXIT
  58.  *    To register hooks which are executed on exit().
  59.  *    Usually a wrapper for STDC atexit.
  60.  *
  61.  *  void *g_memmove(void *dest, const void *src, guint count);
  62.  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
  63.  *    exist.  The prototype looks like the above, give or take a const,
  64.  *    or size_t.
  65.  */
  66. #include <glibconfig.h>
  67.  
  68. /* include varargs functions for assertment macros
  69.  */
  70. #include <stdarg.h>
  71.  
  72. /* optionally feature DMALLOC memory allocation debugger
  73.  */
  74. #ifdef USE_DMALLOC
  75. #include "dmalloc.h"
  76. #endif
  77.  
  78.  
  79. #ifdef NATIVE_WIN32
  80.  
  81. /* On native Win32, directory separator is the backslash, and search path
  82.  * separator is the semicolon.
  83.  */
  84. #define G_DIR_SEPARATOR '\\'
  85. #define G_DIR_SEPARATOR_S "\\"
  86. #define G_SEARCHPATH_SEPARATOR ';'
  87. #define G_SEARCHPATH_SEPARATOR_S ";"
  88.  
  89. #else  /* !NATIVE_WIN32 */
  90.  
  91. #ifndef __EMX__
  92. /* Unix */
  93.  
  94. #define G_DIR_SEPARATOR '/'
  95. #define G_DIR_SEPARATOR_S "/"
  96. #define G_SEARCHPATH_SEPARATOR ':'
  97. #define G_SEARCHPATH_SEPARATOR_S ":"
  98.  
  99. #else
  100. /* EMX/OS2 */
  101.  
  102. #define G_DIR_SEPARATOR '/'
  103. #define G_DIR_SEPARATOR_S "/"
  104. #define G_SEARCHPATH_SEPARATOR ';'
  105. #define G_SEARCHPATH_SEPARATOR_S ";"
  106.  
  107. #endif
  108.  
  109. #endif /* !NATIVE_WIN32 */
  110.  
  111. #ifdef __cplusplus
  112. extern "C" {
  113. #endif /* __cplusplus */
  114.  
  115.  
  116. /* Provide definitions for some commonly used macros.
  117.  *  Some of them are only provided if they haven't already
  118.  *  been defined. It is assumed that if they are already
  119.  *  defined then the current definition is correct.
  120.  */
  121. #ifndef    NULL
  122. #define    NULL    ((void*) 0)
  123. #endif
  124.  
  125. #ifndef    FALSE
  126. #define    FALSE    (0)
  127. #endif
  128.  
  129. #ifndef    TRUE
  130. #define    TRUE    (!FALSE)
  131. #endif
  132.  
  133. #undef    MAX
  134. #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
  135.  
  136. #undef    MIN
  137. #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
  138.  
  139. #undef    ABS
  140. #define ABS(a)       (((a) < 0) ? -(a) : (a))
  141.  
  142. #undef    CLAMP
  143. #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  144.  
  145.  
  146. /* Define G_VA_COPY() to do the right thing for copying va_list variables.
  147.  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
  148.  */
  149. #if !defined (G_VA_COPY)
  150. #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
  151. #  define G_VA_COPY(ap1, ap2)      (*(ap1) = *(ap2))
  152. #  elif defined (G_VA_COPY_AS_ARRAY)
  153. #  define G_VA_COPY(ap1, ap2)      g_memmove ((ap1), (ap2), sizeof (va_list))
  154. #  else /* va_list is a pointer */
  155. #  define G_VA_COPY(ap1, ap2)      ((ap1) = (ap2))
  156. #  endif /* va_list is a pointer */
  157. #endif /* !G_VA_COPY */
  158.  
  159.  
  160. /* Provide convenience macros for handling structure
  161.  * fields through their offsets.
  162.  */
  163. #define G_STRUCT_OFFSET(struct_type, member)    \
  164.     ((gulong) ((gchar*) &((struct_type*) 0)->member))
  165. #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
  166.     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
  167. #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
  168.     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
  169.  
  170.  
  171. /* inlining hassle. for compilers that don't allow the `inline' keyword,
  172.  * mostly because of strict ANSI C compliance or dumbness, we try to fall
  173.  * back to either `__inline__' or `__inline'.
  174.  * we define G_CAN_INLINE, if the compiler seems to be actually
  175.  * *capable* to do function inlining, in which case inline function bodys
  176.  * do make sense. we also define G_INLINE_FUNC to properly export the
  177.  * function prototypes if no inlining can be performed.
  178.  * we special case most of the stuff, so inline functions can have a normal
  179.  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
  180.  */
  181. #ifndef G_INLINE_FUNC
  182. #  define G_CAN_INLINE 1
  183. #endif
  184. #ifdef G_HAVE_INLINE
  185. #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
  186. #    undef inline
  187. #    define inline __inline__
  188. #  endif
  189. #else /* !G_HAVE_INLINE */
  190. #  undef inline
  191. #  if defined (G_HAVE___INLINE__)
  192. #    define inline __inline__
  193. #  else /* !inline && !__inline__ */
  194. #    if defined (G_HAVE___INLINE)
  195. #      define inline __inline
  196. #    else /* !inline && !__inline__ && !__inline */
  197. #      define inline /* don't inline, then */
  198. #      ifndef G_INLINE_FUNC
  199. #     undef G_CAN_INLINE
  200. #      endif
  201. #    endif
  202. #  endif
  203. #endif
  204. #ifndef G_INLINE_FUNC
  205. #  ifdef __GNUC__
  206. #    ifdef __OPTIMIZE__
  207. #      define G_INLINE_FUNC extern inline
  208. #    else
  209. #      undef G_CAN_INLINE
  210. #      define G_INLINE_FUNC extern
  211. #    endif
  212. #  else /* !__GNUC__ */
  213. #    ifdef G_CAN_INLINE
  214. #      define G_INLINE_FUNC static inline
  215. #    else
  216. #      define G_INLINE_FUNC extern
  217. #    endif
  218. #  endif /* !__GNUC__ */
  219. #endif /* !G_INLINE_FUNC */
  220.  
  221.  
  222. /* Provide simple macro statement wrappers (adapted from Perl):
  223.  *  G_STMT_START { statements; } G_STMT_END;
  224.  *  can be used as a single statement, as in
  225.  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
  226.  *
  227.  *  For gcc we will wrap the statements within `({' and `})' braces.
  228.  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
  229.  *  and otherwise within `do' and `while (0)'.
  230.  */
  231. #if !(defined (G_STMT_START) && defined (G_STMT_END))
  232. #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
  233. #    define G_STMT_START    (void)(
  234. #    define G_STMT_END        )
  235. #  else
  236. #    if (defined (sun) || defined (__sun__))
  237. #      define G_STMT_START    if (1)
  238. #      define G_STMT_END    else (void)0
  239. #    else
  240. #      define G_STMT_START    do
  241. #      define G_STMT_END    while (0)
  242. #    endif
  243. #  endif
  244. #endif
  245.  
  246.  
  247. /* Provide macros to feature the GCC function attribute.
  248.  */
  249. #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  250. #define G_GNUC_PRINTF( format_idx, arg_idx )    \
  251.   __attribute__((format (printf, format_idx, arg_idx)))
  252. #define G_GNUC_SCANF( format_idx, arg_idx )    \
  253.   __attribute__((format (scanf, format_idx, arg_idx)))
  254. #define G_GNUC_FORMAT( arg_idx )        \
  255.   __attribute__((format_arg (arg_idx)))
  256. #define G_GNUC_NORETURN                \
  257.   __attribute__((noreturn))
  258. #define G_GNUC_CONST                \
  259.   __attribute__((const))
  260. #define G_GNUC_UNUSED                \
  261.   __attribute__((unused))
  262. #else    /* !__GNUC__ */
  263. #define G_GNUC_PRINTF( format_idx, arg_idx )
  264. #define G_GNUC_SCANF( format_idx, arg_idx )
  265. #define G_GNUC_FORMAT( arg_idx )
  266. #define G_GNUC_NORETURN
  267. #define G_GNUC_CONST
  268. #define    G_GNUC_UNUSED
  269. #endif    /* !__GNUC__ */
  270.  
  271.  
  272. /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
  273.  * macros, so we can refer to them as strings unconditionally.
  274.  */
  275. #ifdef    __GNUC__
  276. #define    G_GNUC_FUNCTION        __FUNCTION__
  277. #define    G_GNUC_PRETTY_FUNCTION    __PRETTY_FUNCTION__
  278. #else    /* !__GNUC__ */
  279. #define    G_GNUC_FUNCTION        ""
  280. #define    G_GNUC_PRETTY_FUNCTION    ""
  281. #endif    /* !__GNUC__ */
  282.  
  283. /* we try to provide a usefull equivalent for ATEXIT if it is
  284.  * not defined, but use is actually abandoned. people should
  285.  * use g_atexit() instead.
  286.  */
  287. #ifndef ATEXIT
  288. # define ATEXIT(proc)    g_ATEXIT(proc)
  289. #else
  290. # define G_NATIVE_ATEXIT
  291. #endif /* ATEXIT */
  292.  
  293. /* Hacker macro to place breakpoints for elected machines.
  294.  * Actual use is strongly deprecated of course ;)
  295.  */
  296. #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  297. #define    G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
  298. #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
  299. #define    G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
  300. #else    /* !__i386__ && !__alpha__ */
  301. #define    G_BREAKPOINT()
  302. #endif    /* __i386__ */
  303.  
  304.  
  305. /* Provide macros for easily allocating memory. The macros
  306.  *  will cast the allocated memory to the specified type
  307.  *  in order to avoid compiler warnings. (Makes the code neater).
  308.  */
  309.  
  310. #ifdef __DMALLOC_H__
  311. #  define g_new(type, count)        (ALLOC (type, count))
  312. #  define g_new0(type, count)        (CALLOC (type, count))
  313. #  define g_renew(type, mem, count)    (REALLOC (mem, type, count))
  314. #else /* __DMALLOC_H__ */
  315. #  define g_new(type, count)      \
  316.       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
  317. #  define g_new0(type, count)      \
  318.       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
  319. #  define g_renew(type, mem, count)      \
  320.       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
  321. #endif /* __DMALLOC_H__ */
  322.  
  323. #define g_mem_chunk_create(type, pre_alloc, alloc_type)    ( \
  324.   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
  325.            sizeof (type), \
  326.            sizeof (type) * (pre_alloc), \
  327.            (alloc_type)) \
  328. )
  329. #define g_chunk_new(type, chunk)    ( \
  330.   (type *) g_mem_chunk_alloc (chunk) \
  331. )
  332. #define g_chunk_new0(type, chunk)    ( \
  333.   (type *) g_mem_chunk_alloc0 (chunk) \
  334. )
  335. #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
  336.   g_mem_chunk_free ((mem_chunk), (mem)); \
  337. } G_STMT_END
  338.  
  339.  
  340. #define g_string(x) #x
  341.  
  342.  
  343. /* Provide macros for error handling. The "assert" macros will
  344.  *  exit on failure. The "return" macros will exit the current
  345.  *  function. Two different definitions are given for the macros
  346.  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
  347.  *  __PRETTY_FUNCTION__ capability.
  348.  */
  349.  
  350. #ifdef G_DISABLE_ASSERT
  351.  
  352. #define g_assert(expr)
  353. #define g_assert_not_reached()
  354.  
  355. #else /* !G_DISABLE_ASSERT */
  356.  
  357. #ifdef __GNUC__
  358.  
  359. #define g_assert(expr)            G_STMT_START{        \
  360.      if (!(expr))                        \
  361.        g_log (G_LOG_DOMAIN,                    \
  362.           G_LOG_LEVEL_ERROR,                \
  363.           "file %s: line %d (%s): assertion failed: (%s)",    \
  364.           __FILE__,                        \
  365.           __LINE__,                        \
  366.           __PRETTY_FUNCTION__,                \
  367.           #expr);            }G_STMT_END
  368.  
  369. #define g_assert_not_reached()        G_STMT_START{        \
  370.      g_log (G_LOG_DOMAIN,                    \
  371.         G_LOG_LEVEL_ERROR,                    \
  372.         "file %s: line %d (%s): should not be reached",    \
  373.         __FILE__,                        \
  374.         __LINE__,                        \
  375.         __PRETTY_FUNCTION__);    }G_STMT_END
  376.  
  377. #else /* !__GNUC__ */
  378.  
  379. #define g_assert(expr)            G_STMT_START{        \
  380.      if (!(expr))                        \
  381.        g_log (G_LOG_DOMAIN,                    \
  382.           G_LOG_LEVEL_ERROR,                \
  383.           "file %s: line %d: assertion failed: (%s)",    \
  384.           __FILE__,                        \
  385.           __LINE__,                        \
  386.           #expr);            }G_STMT_END
  387.  
  388. #define g_assert_not_reached()        G_STMT_START{    \
  389.      g_log (G_LOG_DOMAIN,                \
  390.         G_LOG_LEVEL_ERROR,                \
  391.         "file %s: line %d: should not be reached",    \
  392.         __FILE__,                    \
  393.         __LINE__);        }G_STMT_END
  394.  
  395. #endif /* __GNUC__ */
  396.  
  397. #endif /* !G_DISABLE_ASSERT */
  398.  
  399.  
  400. #ifdef G_DISABLE_CHECKS
  401.  
  402. #define g_return_if_fail(expr)
  403. #define g_return_val_if_fail(expr,val)
  404.  
  405. #else /* !G_DISABLE_CHECKS */
  406.  
  407. #ifdef __GNUC__
  408.  
  409. #define g_return_if_fail(expr)        G_STMT_START{            \
  410.      if (!(expr))                            \
  411.        {                                \
  412.      g_log (G_LOG_DOMAIN,                        \
  413.         G_LOG_LEVEL_CRITICAL,                    \
  414.         "file %s: line %d (%s): assertion `%s' failed.",    \
  415.         __FILE__,                        \
  416.         __LINE__,                        \
  417.         __PRETTY_FUNCTION__,                    \
  418.         #expr);                            \
  419.      return;                            \
  420.        };                }G_STMT_END
  421.  
  422. #define g_return_val_if_fail(expr,val)    G_STMT_START{            \
  423.      if (!(expr))                            \
  424.        {                                \
  425.      g_log (G_LOG_DOMAIN,                        \
  426.         G_LOG_LEVEL_CRITICAL,                    \
  427.         "file %s: line %d (%s): assertion `%s' failed.",    \
  428.         __FILE__,                        \
  429.         __LINE__,                        \
  430.         __PRETTY_FUNCTION__,                    \
  431.         #expr);                            \
  432.      return val;                            \
  433.        };                }G_STMT_END
  434.  
  435. #else /* !__GNUC__ */
  436.  
  437. #define g_return_if_fail(expr)        G_STMT_START{        \
  438.      if (!(expr))                        \
  439.        {                            \
  440.      g_log (G_LOG_DOMAIN,                    \
  441.         G_LOG_LEVEL_CRITICAL,                \
  442.         "file %s: line %d: assertion `%s' failed.",    \
  443.         __FILE__,                    \
  444.         __LINE__,                    \
  445.         #expr);                        \
  446.      return;                        \
  447.        };                }G_STMT_END
  448.  
  449. #define g_return_val_if_fail(expr, val)    G_STMT_START{        \
  450.      if (!(expr))                        \
  451.        {                            \
  452.      g_log (G_LOG_DOMAIN,                    \
  453.         G_LOG_LEVEL_CRITICAL,                \
  454.         "file %s: line %d: assertion `%s' failed.",    \
  455.         __FILE__,                    \
  456.         __LINE__,                    \
  457.         #expr);                        \
  458.      return val;                        \
  459.        };                }G_STMT_END
  460.  
  461. #endif /* !__GNUC__ */
  462.  
  463. #endif /* !G_DISABLE_CHECKS */
  464.  
  465.  
  466. /* Provide type definitions for commonly used types.
  467.  *  These are useful because a "gint8" can be adjusted
  468.  *  to be 1 byte (8 bits) on all platforms. Similarly and
  469.  *  more importantly, "gint32" can be adjusted to be
  470.  *  4 bytes (32 bits) on all platforms.
  471.  */
  472.  
  473. typedef char   gchar;
  474. typedef short  gshort;
  475. typedef long   glong;
  476. typedef int    gint;
  477. typedef gint   gboolean;
  478.  
  479. typedef unsigned char    guchar;
  480. typedef unsigned short    gushort;
  481. typedef unsigned long    gulong;
  482. typedef unsigned int    guint;
  483.  
  484. typedef float    gfloat;
  485. typedef double    gdouble;
  486.  
  487. /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
  488.  * Since gldouble isn't used anywhere, just disable it for now */
  489.  
  490. #if 0
  491. #ifdef HAVE_LONG_DOUBLE
  492. typedef long double gldouble;
  493. #else /* HAVE_LONG_DOUBLE */
  494. typedef double gldouble;
  495. #endif /* HAVE_LONG_DOUBLE */
  496. #endif /* 0 */
  497.  
  498. typedef void* gpointer;
  499. typedef const void *gconstpointer;
  500.  
  501.  
  502. typedef gint32    gssize;
  503. typedef guint32 gsize;
  504. typedef guint32 GQuark;
  505. typedef gint32    GTime;
  506.  
  507.  
  508. /* Portable endian checks and conversions
  509.  *
  510.  * glibconfig.h defines G_BYTE_ORDER which expands to one of
  511.  * the below macros.
  512.  */
  513. #define G_LITTLE_ENDIAN 1234
  514. #define G_BIG_ENDIAN    4321
  515. #define G_PDP_ENDIAN    3412        /* unused, need specific PDP check */    
  516.  
  517.  
  518. /* Basic bit swapping functions
  519.  */
  520. #define GUINT16_SWAP_LE_BE_CONSTANT(val)    ((guint16) ( \
  521.     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
  522.     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
  523. #define GUINT32_SWAP_LE_BE_CONSTANT(val)    ((guint32) ( \
  524.     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
  525.     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
  526.     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
  527.     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
  528.  
  529. /* Intel specific stuff for speed
  530.  */
  531. #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  532. #  define GUINT16_SWAP_LE_BE_X86(val) \
  533.      (__extension__                    \
  534.       ({ register guint16 __v;                \
  535.      if (__builtin_constant_p (val))        \
  536.        __v = GUINT16_SWAP_LE_BE_CONSTANT (val);    \
  537.      else                        \
  538.        __asm__ __const__ ("rorw $8, %w0"        \
  539.                   : "=r" (__v)        \
  540.                   : "0" ((guint16) (val)));    \
  541.     __v; }))
  542. #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
  543. #  if !defined(__i486__) && !defined(__i586__) \
  544.       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
  545. #     define GUINT32_SWAP_LE_BE_X86(val) \
  546.         (__extension__                        \
  547.          ({ register guint32 __v;                \
  548.         if (__builtin_constant_p (val))            \
  549.           __v = GUINT32_SWAP_LE_BE_CONSTANT (val);        \
  550.       else                            \
  551.         __asm__ __const__ ("rorw $8, %w0\n\t"        \
  552.                    "rorl $16, %0\n\t"        \
  553.                    "rorw $8, %w0"            \
  554.                    : "=r" (__v)            \
  555.                    : "0" ((guint32) (val)));    \
  556.     __v; }))
  557. #  else /* 486 and higher has bswap */
  558. #     define GUINT32_SWAP_LE_BE_X86(val) \
  559.         (__extension__                        \
  560.          ({ register guint32 __v;                \
  561.         if (__builtin_constant_p (val))            \
  562.           __v = GUINT32_SWAP_LE_BE_CONSTANT (val);        \
  563.       else                            \
  564.         __asm__ __const__ ("bswap %0"            \
  565.                    : "=r" (__v)            \
  566.                    : "0" ((guint32) (val)));    \
  567.     __v; }))
  568. #  endif /* processor specific 32-bit stuff */
  569. #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
  570. #else /* !__i386__ */
  571. #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
  572. #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
  573. #endif /* __i386__ */
  574.  
  575. #ifdef G_HAVE_GINT64
  576. #  define GUINT64_SWAP_LE_BE_CONSTANT(val)    ((guint64) ( \
  577.       (((guint64) (val) &                        \
  578.     (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |    \
  579.       (((guint64) (val) &                        \
  580.     (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |    \
  581.       (((guint64) (val) &                        \
  582.     (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |    \
  583.       (((guint64) (val) &                        \
  584.     (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |    \
  585.       (((guint64) (val) &                        \
  586.     (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |    \
  587.       (((guint64) (val) &                        \
  588.     (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |    \
  589.       (((guint64) (val) &                        \
  590.     (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |    \
  591.       (((guint64) (val) &                        \
  592.     (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
  593. #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  594. #    define GUINT64_SWAP_LE_BE_X86(val) \
  595.     (__extension__                        \
  596.      ({ union { guint64 __ll;                \
  597.             guint32 __l[2]; } __r;            \
  598.         if (__builtin_constant_p (val))            \
  599.           __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);    \
  600.         else                        \
  601.           {                            \
  602.          union { guint64 __ll;                \
  603.             guint32 __l[2]; } __w;            \
  604.         __w.__ll = ((guint64) val);            \
  605.         __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);    \
  606.         __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);    \
  607.           }                            \
  608.       __r.__ll; }))
  609. #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
  610. #  else /* !__i386__ */
  611. #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
  612. #  endif
  613. #endif
  614.  
  615. #define GUINT16_SWAP_LE_PDP(val)    ((guint16) (val))
  616. #define GUINT16_SWAP_BE_PDP(val)    (GUINT16_SWAP_LE_BE (val))
  617. #define GUINT32_SWAP_LE_PDP(val)    ((guint32) ( \
  618.     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
  619.     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
  620. #define GUINT32_SWAP_BE_PDP(val)    ((guint32) ( \
  621.     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
  622.     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
  623.  
  624. /* The G*_TO_?E() macros are defined in glibconfig.h.
  625.  * The transformation is symmetric, so the FROM just maps to the TO.
  626.  */
  627. #define GINT16_FROM_LE(val)    (GINT16_TO_LE (val))
  628. #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
  629. #define GINT16_FROM_BE(val)    (GINT16_TO_BE (val))
  630. #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
  631. #define GINT32_FROM_LE(val)    (GINT32_TO_LE (val))
  632. #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
  633. #define GINT32_FROM_BE(val)    (GINT32_TO_BE (val))
  634. #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
  635.  
  636. #ifdef G_HAVE_GINT64
  637. #define GINT64_FROM_LE(val)    (GINT64_TO_LE (val))
  638. #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
  639. #define GINT64_FROM_BE(val)    (GINT64_TO_BE (val))
  640. #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
  641. #endif
  642.  
  643. #define GLONG_FROM_LE(val)    (GLONG_TO_LE (val))
  644. #define GULONG_FROM_LE(val)    (GULONG_TO_LE (val))
  645. #define GLONG_FROM_BE(val)    (GLONG_TO_BE (val))
  646. #define GULONG_FROM_BE(val)    (GULONG_TO_BE (val))
  647.  
  648. #define GINT_FROM_LE(val)    (GINT_TO_LE (val))
  649. #define GUINT_FROM_LE(val)    (GUINT_TO_LE (val))
  650. #define GINT_FROM_BE(val)    (GINT_TO_BE (val))
  651. #define GUINT_FROM_BE(val)    (GUINT_TO_BE (val))
  652.  
  653.  
  654. /* Portable versions of host-network order stuff
  655.  */
  656. #define g_ntohl(val) (GUINT32_FROM_BE (val))
  657. #define g_ntohs(val) (GUINT16_FROM_BE (val))
  658. #define g_htonl(val) (GUINT32_TO_BE (val))
  659. #define g_htons(val) (GUINT16_TO_BE (val))
  660.  
  661.  
  662. /* Glib version.
  663.  * we prefix variable declarations so they can
  664.  * properly get exported in windows dlls.
  665.  */
  666. #ifdef NATIVE_WIN32
  667. #  ifdef GLIB_COMPILATION
  668. #    define GUTILS_C_VAR __declspec(dllexport)
  669. #  else /* !GLIB_COMPILATION */
  670. #    define GUTILS_C_VAR extern __declspec(dllimport)
  671. #  endif /* !GLIB_COMPILATION */
  672. #else /* !NATIVE_WIN32 */
  673. #  define GUTILS_C_VAR extern
  674. #endif /* !NATIVE_WIN32 */
  675.  
  676. GUTILS_C_VAR const guint glib_major_version;
  677. GUTILS_C_VAR const guint glib_minor_version;
  678. GUTILS_C_VAR const guint glib_micro_version;
  679. GUTILS_C_VAR const guint glib_interface_age;
  680. GUTILS_C_VAR const guint glib_binary_age;
  681.  
  682. #define GLIB_CHECK_VERSION(major,minor,micro)    \
  683.     (GLIB_MAJOR_VERSION > (major) || \
  684.      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
  685.      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
  686.       GLIB_MICRO_VERSION >= (micro)))
  687.  
  688. /* Forward declarations of glib types.
  689.  */
  690. typedef struct _GAllocator    GAllocator;
  691. typedef struct _GArray        GArray;
  692. typedef struct _GByteArray    GByteArray;
  693. typedef struct _GCache        GCache;
  694. typedef struct _GCompletion    GCompletion;
  695. typedef    struct _GData        GData;
  696. typedef struct _GDebugKey    GDebugKey;
  697. typedef struct _GHashTable    GHashTable;
  698. typedef struct _GHook        GHook;
  699. typedef struct _GHookList    GHookList;
  700. typedef struct _GList        GList;
  701. typedef struct _GMemChunk    GMemChunk;
  702. typedef struct _GNode        GNode;
  703. typedef struct _GPtrArray    GPtrArray;
  704. typedef struct _GRelation    GRelation;
  705. typedef struct _GScanner    GScanner;
  706. typedef struct _GScannerConfig    GScannerConfig;
  707. typedef struct _GSList        GSList;
  708. typedef struct _GString        GString;
  709. typedef struct _GStringChunk    GStringChunk;
  710. typedef struct _GTimer        GTimer;
  711. typedef struct _GTree        GTree;
  712. typedef struct _GTuples        GTuples;
  713. typedef union  _GTokenValue    GTokenValue;
  714. typedef struct _GIOChannel    GIOChannel;
  715.  
  716. /* Tree traverse flags */
  717. typedef enum
  718. {
  719.   G_TRAVERSE_LEAFS    = 1 << 0,
  720.   G_TRAVERSE_NON_LEAFS    = 1 << 1,
  721.   G_TRAVERSE_ALL    = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
  722.   G_TRAVERSE_MASK    = 0x03
  723. } GTraverseFlags;
  724.  
  725. /* Tree traverse orders */
  726. typedef enum
  727. {
  728.   G_IN_ORDER,
  729.   G_PRE_ORDER,
  730.   G_POST_ORDER,
  731.   G_LEVEL_ORDER
  732. } GTraverseType;
  733.  
  734. /* Log level shift offset for user defined
  735.  * log levels (0-7 are used by GLib).
  736.  */
  737. #define    G_LOG_LEVEL_USER_SHIFT    (8)
  738.  
  739. /* Glib log levels and flags.
  740.  */
  741. typedef enum
  742. {
  743.   /* log flags */
  744.   G_LOG_FLAG_RECURSION        = 1 << 0,
  745.   G_LOG_FLAG_FATAL        = 1 << 1,
  746.   
  747.   /* GLib log levels */
  748.   G_LOG_LEVEL_ERROR        = 1 << 2,    /* always fatal */
  749.   G_LOG_LEVEL_CRITICAL        = 1 << 3,
  750.   G_LOG_LEVEL_WARNING        = 1 << 4,
  751.   G_LOG_LEVEL_MESSAGE        = 1 << 5,
  752.   G_LOG_LEVEL_INFO        = 1 << 6,
  753.   G_LOG_LEVEL_DEBUG        = 1 << 7,
  754.   
  755.   G_LOG_LEVEL_MASK        = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
  756. } GLogLevelFlags;
  757.  
  758. /* GLib log levels that are considered fatal by default */
  759. #define    G_LOG_FATAL_MASK    (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
  760.  
  761.  
  762. typedef gpointer    (*GCacheNewFunc)    (gpointer    key);
  763. typedef gpointer    (*GCacheDupFunc)    (gpointer    value);
  764. typedef void        (*GCacheDestroyFunc)    (gpointer    value);
  765. typedef gint        (*GCompareFunc)        (gconstpointer    a,
  766.                          gconstpointer    b);
  767. typedef gchar*        (*GCompletionFunc)    (gpointer);
  768. typedef void        (*GDestroyNotify)    (gpointer    data);
  769. typedef void        (*GDataForeachFunc)    (GQuark        key_id,
  770.                          gpointer    data,
  771.                          gpointer    user_data);
  772. typedef void        (*GFunc)        (gpointer    data,
  773.                          gpointer    user_data);
  774. typedef guint        (*GHashFunc)        (gconstpointer    key);
  775. typedef void        (*GFreeFunc)        (gpointer    data);
  776. typedef void        (*GHFunc)        (gpointer    key,
  777.                          gpointer    value,
  778.                          gpointer    user_data);
  779. typedef gboolean    (*GHRFunc)        (gpointer    key,
  780.                          gpointer    value,
  781.                          gpointer    user_data);
  782. typedef gint        (*GHookCompareFunc)    (GHook        *new_hook,
  783.                          GHook        *sibling);
  784. typedef gboolean    (*GHookFindFunc)    (GHook        *hook,
  785.                          gpointer     data);
  786. typedef void        (*GHookMarshaller)    (GHook        *hook,
  787.                          gpointer     data);
  788. typedef gboolean    (*GHookCheckMarshaller)    (GHook        *hook,
  789.                          gpointer     data);
  790. typedef void        (*GHookFunc)        (gpointer     data);
  791. typedef gboolean    (*GHookCheckFunc)    (gpointer     data);
  792. typedef void        (*GHookFreeFunc)    (GHookList      *hook_list,
  793.                          GHook          *hook);
  794. typedef void        (*GLogFunc)        (const gchar   *log_domain,
  795.                          GLogLevelFlags    log_level,
  796.                          const gchar   *message,
  797.                          gpointer    user_data);
  798. typedef gboolean    (*GNodeTraverseFunc)    (GNode           *node,
  799.                          gpointer    data);
  800. typedef void        (*GNodeForeachFunc)    (GNode           *node,
  801.                          gpointer    data);
  802. typedef gint        (*GSearchFunc)        (gpointer    key,
  803.                          gpointer    data);
  804. typedef void        (*GScannerMsgFunc)    (GScanner      *scanner,
  805.                          gchar           *message,
  806.                          gint        error);
  807. typedef gint        (*GTraverseFunc)    (gpointer    key,
  808.                          gpointer    value,
  809.                          gpointer    data);
  810. typedef    void        (*GVoidFunc)        (void);
  811.  
  812.  
  813. struct _GList
  814. {
  815.   gpointer data;
  816.   GList *next;
  817.   GList *prev;
  818. };
  819.  
  820. struct _GSList
  821. {
  822.   gpointer data;
  823.   GSList *next;
  824. };
  825.  
  826. struct _GString
  827. {
  828.   gchar *str;
  829.   gint len;
  830. };
  831.  
  832. struct _GArray
  833. {
  834.   gchar *data;
  835.   guint len;
  836. };
  837.  
  838. struct _GByteArray
  839. {
  840.   guint8 *data;
  841.   guint      len;
  842. };
  843.  
  844. struct _GPtrArray
  845. {
  846.   gpointer *pdata;
  847.   guint        len;
  848. };
  849.  
  850. struct _GTuples
  851. {
  852.   guint len;
  853. };
  854.  
  855. struct _GDebugKey
  856. {
  857.   gchar *key;
  858.   guint     value;
  859. };
  860.  
  861.  
  862. /* Doubly linked lists
  863.  */
  864. void   g_list_push_allocator    (GAllocator     *allocator);
  865. void   g_list_pop_allocator     (void);
  866. GList* g_list_alloc        (void);
  867. void   g_list_free        (GList        *list);
  868. void   g_list_free_1        (GList        *list);
  869. GList* g_list_append        (GList        *list,
  870.                  gpointer     data);
  871. GList* g_list_prepend        (GList        *list,
  872.                  gpointer     data);
  873. GList* g_list_insert        (GList        *list,
  874.                  gpointer     data,
  875.                  gint         position);
  876. GList* g_list_insert_sorted    (GList        *list,
  877.                  gpointer     data,
  878.                  GCompareFunc     func);
  879. GList* g_list_concat        (GList        *list1,
  880.                  GList        *list2);
  881. GList* g_list_remove        (GList        *list,
  882.                  gpointer     data);
  883. GList* g_list_remove_link    (GList        *list,
  884.                  GList        *llink);
  885. GList* g_list_reverse        (GList        *list);
  886. GList* g_list_copy        (GList        *list);
  887. GList* g_list_nth        (GList        *list,
  888.                  guint         n);
  889. GList* g_list_find        (GList        *list,
  890.                  gpointer     data);
  891. GList* g_list_find_custom    (GList        *list,
  892.                  gpointer     data,
  893.                  GCompareFunc     func);
  894. gint   g_list_position        (GList        *list,
  895.                  GList        *llink);
  896. gint   g_list_index        (GList        *list,
  897.                  gpointer     data);
  898. GList* g_list_last        (GList        *list);
  899. GList* g_list_first        (GList        *list);
  900. guint  g_list_length        (GList        *list);
  901. void   g_list_foreach        (GList        *list,
  902.                  GFunc         func,
  903.                  gpointer     user_data);
  904. GList* g_list_sort              (GList          *list,
  905.                          GCompareFunc    compare_func);
  906. gpointer g_list_nth_data    (GList        *list,
  907.                  guint         n);
  908. #define g_list_previous(list)    ((list) ? (((GList *)(list))->prev) : NULL)
  909. #define g_list_next(list)    ((list) ? (((GList *)(list))->next) : NULL)
  910.  
  911.  
  912. /* Singly linked lists
  913.  */
  914. void    g_slist_push_allocator  (GAllocator     *allocator);
  915. void    g_slist_pop_allocator   (void);
  916. GSList* g_slist_alloc        (void);
  917. void    g_slist_free        (GSList        *list);
  918. void    g_slist_free_1        (GSList        *list);
  919. GSList* g_slist_append        (GSList        *list,
  920.                  gpointer     data);
  921. GSList* g_slist_prepend        (GSList        *list,
  922.                  gpointer     data);
  923. GSList* g_slist_insert        (GSList        *list,
  924.                  gpointer     data,
  925.                  gint         position);
  926. GSList* g_slist_insert_sorted    (GSList        *list,
  927.                  gpointer     data,
  928.                  GCompareFunc     func);
  929. GSList* g_slist_concat        (GSList        *list1,
  930.                  GSList        *list2);
  931. GSList* g_slist_remove        (GSList        *list,
  932.                  gpointer     data);
  933. GSList* g_slist_remove_link    (GSList        *list,
  934.                  GSList        *llink);
  935. GSList* g_slist_reverse        (GSList        *list);
  936. GSList*    g_slist_copy        (GSList        *list);
  937. GSList* g_slist_nth        (GSList        *list,
  938.                  guint         n);
  939. GSList* g_slist_find        (GSList        *list,
  940.                  gpointer     data);
  941. GSList* g_slist_find_custom    (GSList        *list,
  942.                  gpointer     data,
  943.                  GCompareFunc     func);
  944. gint    g_slist_position    (GSList        *list,
  945.                  GSList        *llink);
  946. gint    g_slist_index        (GSList        *list,
  947.                  gpointer     data);
  948. GSList* g_slist_last        (GSList        *list);
  949. guint    g_slist_length        (GSList        *list);
  950. void    g_slist_foreach        (GSList        *list,
  951.                  GFunc         func,
  952.                  gpointer     user_data);
  953. GSList*  g_slist_sort           (GSList          *list,
  954.                          GCompareFunc    compare_func);
  955. gpointer g_slist_nth_data    (GSList        *list,
  956.                  guint         n);
  957. #define g_slist_next(slist)    ((slist) ? (((GSList *)(slist))->next) : NULL)
  958.  
  959.  
  960. /* Hash tables
  961.  */
  962. GHashTable* g_hash_table_new        (GHashFunc     hash_func,
  963.                      GCompareFunc     key_compare_func);
  964. void        g_hash_table_destroy    (GHashTable    *hash_table);
  965. void        g_hash_table_insert        (GHashTable    *hash_table,
  966.                      gpointer     key,
  967.                      gpointer     value);
  968. void        g_hash_table_remove        (GHashTable    *hash_table,
  969.                      gconstpointer     key);
  970. gpointer    g_hash_table_lookup        (GHashTable    *hash_table,
  971.                      gconstpointer     key);
  972. gboolean    g_hash_table_lookup_extended(GHashTable    *hash_table,
  973.                      gconstpointer     lookup_key,
  974.                      gpointer    *orig_key,
  975.                      gpointer    *value);
  976. void        g_hash_table_freeze        (GHashTable    *hash_table);
  977. void        g_hash_table_thaw        (GHashTable    *hash_table);
  978. void        g_hash_table_foreach    (GHashTable    *hash_table,
  979.                      GHFunc         func,
  980.                      gpointer     user_data);
  981. guint        g_hash_table_foreach_remove    (GHashTable    *hash_table,
  982.                      GHRFunc     func,
  983.                      gpointer     user_data);
  984. guint        g_hash_table_size        (GHashTable    *hash_table);
  985.  
  986.  
  987. /* Caches
  988.  */
  989. GCache*     g_cache_new           (GCacheNewFunc       value_new_func,
  990.                 GCacheDestroyFunc  value_destroy_func,
  991.                 GCacheDupFunc       key_dup_func,
  992.                 GCacheDestroyFunc  key_destroy_func,
  993.                 GHashFunc       hash_key_func,
  994.                 GHashFunc       hash_value_func,
  995.                 GCompareFunc       key_compare_func);
  996. void     g_cache_destroy       (GCache          *cache);
  997. gpointer g_cache_insert           (GCache          *cache,
  998.                 gpointer       key);
  999. void     g_cache_remove           (GCache          *cache,
  1000.                 gpointer       value);
  1001. void     g_cache_key_foreach   (GCache          *cache,
  1002.                 GHFunc           func,
  1003.                 gpointer       user_data);
  1004. void     g_cache_value_foreach (GCache          *cache,
  1005.                 GHFunc           func,
  1006.                 gpointer       user_data);
  1007.  
  1008.  
  1009. /* Balanced binary trees
  1010.  */
  1011. GTree*     g_tree_new     (GCompareFunc     key_compare_func);
  1012. void     g_tree_destroy     (GTree        *tree);
  1013. void     g_tree_insert     (GTree        *tree,
  1014.               gpointer     key,
  1015.               gpointer     value);
  1016. void     g_tree_remove     (GTree        *tree,
  1017.               gpointer     key);
  1018. gpointer g_tree_lookup     (GTree        *tree,
  1019.               gpointer     key);
  1020. void     g_tree_traverse (GTree        *tree,
  1021.               GTraverseFunc     traverse_func,
  1022.               GTraverseType     traverse_type,
  1023.               gpointer     data);
  1024. gpointer g_tree_search     (GTree        *tree,
  1025.               GSearchFunc     search_func,
  1026.               gpointer     data);
  1027. gint     g_tree_height     (GTree        *tree);
  1028. gint     g_tree_nnodes     (GTree        *tree);
  1029.  
  1030.  
  1031.  
  1032. /* N-way tree implementation
  1033.  */
  1034. struct _GNode
  1035. {
  1036.   gpointer data;
  1037.   GNode      *next;
  1038.   GNode      *prev;
  1039.   GNode      *parent;
  1040.   GNode      *children;
  1041. };
  1042.  
  1043. #define     G_NODE_IS_ROOT(node)    (((GNode*) (node))->parent == NULL && \
  1044.                  ((GNode*) (node))->prev == NULL && \
  1045.                  ((GNode*) (node))->next == NULL)
  1046. #define     G_NODE_IS_LEAF(node)    (((GNode*) (node))->children == NULL)
  1047.  
  1048. void     g_node_push_allocator  (GAllocator       *allocator);
  1049. void     g_node_pop_allocator   (void);
  1050. GNode*     g_node_new        (gpointer       data);
  1051. void     g_node_destroy        (GNode          *root);
  1052. void     g_node_unlink        (GNode          *node);
  1053. GNode*     g_node_insert        (GNode          *parent,
  1054.                  gint           position,
  1055.                  GNode          *node);
  1056. GNode*     g_node_insert_before    (GNode          *parent,
  1057.                  GNode          *sibling,
  1058.                  GNode          *node);
  1059. GNode*     g_node_prepend        (GNode          *parent,
  1060.                  GNode          *node);
  1061. guint     g_node_n_nodes        (GNode          *root,
  1062.                  GTraverseFlags       flags);
  1063. GNode*     g_node_get_root    (GNode          *node);
  1064. gboolean g_node_is_ancestor    (GNode          *node,
  1065.                  GNode          *descendant);
  1066. guint     g_node_depth        (GNode          *node);
  1067. GNode*     g_node_find        (GNode          *root,
  1068.                  GTraverseType       order,
  1069.                  GTraverseFlags       flags,
  1070.                  gpointer       data);
  1071.  
  1072. /* convenience macros */
  1073. #define g_node_append(parent, node)                \
  1074.      g_node_insert_before ((parent), NULL, (node))
  1075. #define    g_node_insert_data(parent, position, data)        \
  1076.      g_node_insert ((parent), (position), g_node_new (data))
  1077. #define    g_node_insert_data_before(parent, sibling, data)    \
  1078.      g_node_insert_before ((parent), (sibling), g_node_new (data))
  1079. #define    g_node_prepend_data(parent, data)            \
  1080.      g_node_prepend ((parent), g_node_new (data))
  1081. #define    g_node_append_data(parent, data)            \
  1082.      g_node_insert_before ((parent), NULL, g_node_new (data))
  1083.  
  1084. /* traversal function, assumes that `node' is root
  1085.  * (only traverses `node' and its subtree).
  1086.  * this function is just a high level interface to
  1087.  * low level traversal functions, optimized for speed.
  1088.  */
  1089. void     g_node_traverse    (GNode          *root,
  1090.                  GTraverseType       order,
  1091.                  GTraverseFlags       flags,
  1092.                  gint           max_depth,
  1093.                  GNodeTraverseFunc func,
  1094.                  gpointer       data);
  1095.  
  1096. /* return the maximum tree height starting with `node', this is an expensive
  1097.  * operation, since we need to visit all nodes. this could be shortened by
  1098.  * adding `guint height' to struct _GNode, but then again, this is not very
  1099.  * often needed, and would make g_node_insert() more time consuming.
  1100.  */
  1101. guint     g_node_max_height     (GNode *root);
  1102.  
  1103. void     g_node_children_foreach (GNode          *node,
  1104.                   GTraverseFlags   flags,
  1105.                   GNodeForeachFunc func,
  1106.                   gpointer       data);
  1107. void     g_node_reverse_children (GNode          *node);
  1108. guint     g_node_n_children     (GNode          *node);
  1109. GNode*     g_node_nth_child     (GNode          *node,
  1110.                   guint           n);
  1111. GNode*     g_node_last_child     (GNode          *node);
  1112. GNode*     g_node_find_child     (GNode          *node,
  1113.                   GTraverseFlags   flags,
  1114.                   gpointer       data);
  1115. gint     g_node_child_position     (GNode          *node,
  1116.                   GNode          *child);
  1117. gint     g_node_child_index     (GNode          *node,
  1118.                   gpointer       data);
  1119.  
  1120. GNode*     g_node_first_sibling     (GNode          *node);
  1121. GNode*     g_node_last_sibling     (GNode          *node);
  1122.  
  1123. #define     g_node_prev_sibling(node)    ((node) ? \
  1124.                      ((GNode*) (node))->prev : NULL)
  1125. #define     g_node_next_sibling(node)    ((node) ? \
  1126.                      ((GNode*) (node))->next : NULL)
  1127. #define     g_node_first_child(node)    ((node) ? \
  1128.                      ((GNode*) (node))->children : NULL)
  1129.  
  1130.  
  1131. /* Callback maintenance functions
  1132.  */
  1133. #define G_HOOK_FLAG_USER_SHIFT    (4)
  1134. typedef enum
  1135. {
  1136.   G_HOOK_FLAG_ACTIVE    = 1 << 0,
  1137.   G_HOOK_FLAG_IN_CALL    = 1 << 1,
  1138.   G_HOOK_FLAG_MASK    = 0x0f
  1139. } GHookFlagMask;
  1140.  
  1141. #define    G_HOOK_DEFERRED_DESTROY    ((GHookFreeFunc) 0x01)
  1142.  
  1143. struct _GHookList
  1144. {
  1145.   guint         seq_id;
  1146.   guint         hook_size;
  1147.   guint         is_setup : 1;
  1148.   GHook        *hooks;
  1149.   GMemChunk    *hook_memchunk;
  1150.   GHookFreeFunc     hook_free; /* virtual function */
  1151.   GHookFreeFunc     hook_destroy; /* virtual function */
  1152. };
  1153.  
  1154. struct _GHook
  1155. {
  1156.   gpointer     data;
  1157.   GHook        *next;
  1158.   GHook        *prev;
  1159.   guint         ref_count;
  1160.   guint         hook_id;
  1161.   guint         flags;
  1162.   gpointer     func;
  1163.   GDestroyNotify destroy;
  1164. };
  1165.  
  1166. #define    G_HOOK_ACTIVE(hook)        ((((GHook*) hook)->flags & \
  1167.                       G_HOOK_FLAG_ACTIVE) != 0)
  1168. #define    G_HOOK_IN_CALL(hook)        ((((GHook*) hook)->flags & \
  1169.                       G_HOOK_FLAG_IN_CALL) != 0)
  1170. #define G_HOOK_IS_VALID(hook)        (((GHook*) hook)->hook_id != 0 && \
  1171.                      G_HOOK_ACTIVE (hook))
  1172. #define G_HOOK_IS_UNLINKED(hook)    (((GHook*) hook)->next == NULL && \
  1173.                      ((GHook*) hook)->prev == NULL && \
  1174.                      ((GHook*) hook)->hook_id == 0 && \
  1175.                      ((GHook*) hook)->ref_count == 0)
  1176.  
  1177. void     g_hook_list_init        (GHookList        *hook_list,
  1178.                      guint             hook_size);
  1179. void     g_hook_list_clear        (GHookList        *hook_list);
  1180. GHook*     g_hook_alloc            (GHookList        *hook_list);
  1181. void     g_hook_free            (GHookList        *hook_list,
  1182.                      GHook            *hook);
  1183. void     g_hook_ref            (GHookList        *hook_list,
  1184.                      GHook            *hook);
  1185. void     g_hook_unref            (GHookList        *hook_list,
  1186.                      GHook            *hook);
  1187. gboolean g_hook_destroy            (GHookList        *hook_list,
  1188.                      guint             hook_id);
  1189. void     g_hook_destroy_link        (GHookList        *hook_list,
  1190.                      GHook            *hook);
  1191. void     g_hook_prepend            (GHookList        *hook_list,
  1192.                      GHook            *hook);
  1193. void     g_hook_insert_before        (GHookList        *hook_list,
  1194.                      GHook            *sibling,
  1195.                      GHook            *hook);
  1196. void     g_hook_insert_sorted        (GHookList        *hook_list,
  1197.                      GHook            *hook,
  1198.                      GHookCompareFunc     func);
  1199. GHook*     g_hook_get            (GHookList        *hook_list,
  1200.                      guint             hook_id);
  1201. GHook*     g_hook_find            (GHookList        *hook_list,
  1202.                      gboolean         need_valids,
  1203.                      GHookFindFunc         func,
  1204.                      gpointer         data);
  1205. GHook*     g_hook_find_data        (GHookList        *hook_list,
  1206.                      gboolean         need_valids,
  1207.                      gpointer         data);
  1208. GHook*     g_hook_find_func        (GHookList        *hook_list,
  1209.                      gboolean         need_valids,
  1210.                      gpointer         func);
  1211. GHook*     g_hook_find_func_data        (GHookList        *hook_list,
  1212.                      gboolean         need_valids,
  1213.                      gpointer         func,
  1214.                      gpointer         data);
  1215. /* return the first valid hook, and increment its reference count */
  1216. GHook*     g_hook_first_valid        (GHookList        *hook_list,
  1217.                      gboolean         may_be_in_call);
  1218. /* return the next valid hook with incremented reference count, and
  1219.  * decrement the reference count of the original hook
  1220.  */
  1221. GHook*     g_hook_next_valid        (GHookList        *hook_list,
  1222.                      GHook            *hook,
  1223.                      gboolean         may_be_in_call);
  1224.  
  1225. /* GHookCompareFunc implementation to insert hooks sorted by their id */
  1226. gint     g_hook_compare_ids        (GHook            *new_hook,
  1227.                      GHook            *sibling);
  1228.  
  1229. /* convenience macros */
  1230. #define     g_hook_append( hook_list, hook )  \
  1231.      g_hook_insert_before ((hook_list), NULL, (hook))
  1232.  
  1233. /* invoke all valid hooks with the (*GHookFunc) signature.
  1234.  */
  1235. void     g_hook_list_invoke        (GHookList        *hook_list,
  1236.                      gboolean         may_recurse);
  1237. /* invoke all valid hooks with the (*GHookCheckFunc) signature,
  1238.  * and destroy the hook if FALSE is returned.
  1239.  */
  1240. void     g_hook_list_invoke_check    (GHookList        *hook_list,
  1241.                      gboolean         may_recurse);
  1242. /* invoke a marshaller on all valid hooks.
  1243.  */
  1244. void     g_hook_list_marshal        (GHookList        *hook_list,
  1245.                      gboolean         may_recurse,
  1246.                      GHookMarshaller     marshaller,
  1247.                      gpointer         data);
  1248. void     g_hook_list_marshal_check    (GHookList        *hook_list,
  1249.                      gboolean         may_recurse,
  1250.                      GHookCheckMarshaller     marshaller,
  1251.                      gpointer         data);
  1252.  
  1253.  
  1254. /* Fatal error handlers.
  1255.  * g_on_error_query() will prompt the user to either
  1256.  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
  1257.  * g_on_error_stack_trace() invokes gdb, which attaches to the current
  1258.  * process and shows a stack trace.
  1259.  * These function may cause different actions on non-unix platforms.
  1260.  * The prg_name arg is required by gdb to find the executable, if it is
  1261.  * passed as NULL, g_on_error_query() will try g_get_prgname().
  1262.  */
  1263. void g_on_error_query (const gchar *prg_name);
  1264. void g_on_error_stack_trace (const gchar *prg_name);
  1265.  
  1266.  
  1267. /* Logging mechanism
  1268.  */
  1269. extern            const gchar        *g_log_domain_glib;
  1270. guint        g_log_set_handler    (const gchar    *log_domain,
  1271.                      GLogLevelFlags     log_levels,
  1272.                      GLogFunc     log_func,
  1273.                      gpointer     user_data);
  1274. void        g_log_remove_handler    (const gchar    *log_domain,
  1275.                      guint         handler_id);
  1276. void        g_log_default_handler    (const gchar    *log_domain,
  1277.                      GLogLevelFlags     log_level,
  1278.                      const gchar    *message,
  1279.                      gpointer     unused_data);
  1280. void        g_log            (const gchar    *log_domain,
  1281.                      GLogLevelFlags     log_level,
  1282.                      const gchar    *format,
  1283.                      ...) G_GNUC_PRINTF (3, 4);
  1284. void        g_logv            (const gchar    *log_domain,
  1285.                      GLogLevelFlags     log_level,
  1286.                      const gchar    *format,
  1287.                      va_list     args);
  1288. GLogLevelFlags    g_log_set_fatal_mask    (const gchar    *log_domain,
  1289.                      GLogLevelFlags     fatal_mask);
  1290. GLogLevelFlags    g_log_set_always_fatal    (GLogLevelFlags     fatal_mask);
  1291. #ifndef    G_LOG_DOMAIN
  1292. #define    G_LOG_DOMAIN    ((gchar*) 0)
  1293. #endif    /* G_LOG_DOMAIN */
  1294. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  1295. #define    g_error(...)    g_log (G_LOG_DOMAIN,         \
  1296.                    G_LOG_LEVEL_ERROR,    \
  1297.                    __VA_ARGS__)
  1298. #define    g_message(...)    g_log (G_LOG_DOMAIN,         \
  1299.                    G_LOG_LEVEL_MESSAGE,  \
  1300.                    __VA_ARGS__)
  1301. #define    g_critical(...)    g_log (G_LOG_DOMAIN,         \
  1302.                    G_LOG_LEVEL_CRITICAL, \
  1303.                    __VA_ARGS__)
  1304. #define    g_warning(...)    g_log (G_LOG_DOMAIN,         \
  1305.                    G_LOG_LEVEL_WARNING,  \
  1306.                    __VA_ARGS__)
  1307. #elif defined (__GNUC__)
  1308. #define    g_error(format...)    g_log (G_LOG_DOMAIN,         \
  1309.                        G_LOG_LEVEL_ERROR,    \
  1310.                        format)
  1311. #define    g_message(format...)    g_log (G_LOG_DOMAIN,         \
  1312.                        G_LOG_LEVEL_MESSAGE,  \
  1313.                        format)
  1314. #define    g_critical(format...)    g_log (G_LOG_DOMAIN,         \
  1315.                        G_LOG_LEVEL_CRITICAL, \
  1316.                        format)
  1317. #define    g_warning(format...)    g_log (G_LOG_DOMAIN,         \
  1318.                        G_LOG_LEVEL_WARNING,  \
  1319.                        format)
  1320. #else    /* !__GNUC__ */
  1321. static void
  1322. g_error (const gchar *format,
  1323.      ...)
  1324. {
  1325.   va_list args;
  1326.   va_start (args, format);
  1327.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
  1328.   va_end (args);
  1329. }
  1330. static void
  1331. g_message (const gchar *format,
  1332.        ...)
  1333. {
  1334.   va_list args;
  1335.   va_start (args, format);
  1336.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  1337.   va_end (args);
  1338. }
  1339. static void
  1340. g_warning (const gchar *format,
  1341.        ...)
  1342. {
  1343.   va_list args;
  1344.   va_start (args, format);
  1345.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
  1346.   va_end (args);
  1347. }
  1348. #endif    /* !__GNUC__ */
  1349.  
  1350. typedef void    (*GPrintFunc)        (const gchar    *string);
  1351. void        g_print            (const gchar    *format,
  1352.                      ...) G_GNUC_PRINTF (1, 2);
  1353. GPrintFunc    g_set_print_handler    (GPrintFunc     func);
  1354. void        g_printerr        (const gchar    *format,
  1355.                      ...) G_GNUC_PRINTF (1, 2);
  1356. GPrintFunc    g_set_printerr_handler    (GPrintFunc     func);
  1357.  
  1358. /* deprecated compatibility functions, use g_log_set_handler() instead */
  1359. typedef void        (*GErrorFunc)        (const gchar *str);
  1360. typedef void        (*GWarningFunc)        (const gchar *str);
  1361. GErrorFunc   g_set_error_handler   (GErrorFunc     func);
  1362. GWarningFunc g_set_warning_handler (GWarningFunc func);
  1363. GPrintFunc   g_set_message_handler (GPrintFunc func);
  1364.  
  1365.  
  1366. /* Memory allocation and debugging
  1367.  */
  1368. #ifdef USE_DMALLOC
  1369.  
  1370. #define g_malloc(size)         ((gpointer) MALLOC (size))
  1371. #define g_malloc0(size)         ((gpointer) CALLOC (char, size))
  1372. #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
  1373. #define g_free(mem)         FREE (mem)
  1374.  
  1375. #else /* !USE_DMALLOC */
  1376.  
  1377. gpointer g_malloc      (gulong      size);
  1378. gpointer g_malloc0     (gulong      size);
  1379. gpointer g_realloc     (gpointer  mem,
  1380.             gulong      size);
  1381. void     g_free           (gpointer  mem);
  1382.  
  1383. #endif /* !USE_DMALLOC */
  1384.  
  1385. void     g_mem_profile (void);
  1386. void     g_mem_check   (gpointer  mem);
  1387.  
  1388. /* Generic allocators
  1389.  */
  1390. GAllocator* g_allocator_new   (const gchar  *name,
  1391.                    guint         n_preallocs);
  1392. void        g_allocator_free  (GAllocator   *allocator);
  1393.  
  1394. #define    G_ALLOCATOR_LIST    (1)
  1395. #define    G_ALLOCATOR_SLIST    (2)
  1396. #define    G_ALLOCATOR_NODE    (3)
  1397.  
  1398.  
  1399. /* "g_mem_chunk_new" creates a new memory chunk.
  1400.  * Memory chunks are used to allocate pieces of memory which are
  1401.  *  always the same size. Lists are a good example of such a data type.
  1402.  * The memory chunk allocates and frees blocks of memory as needed.
  1403.  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
  1404.  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
  1405.  *  fault...somewhere).
  1406.  *
  1407.  * Oh yeah, GMemChunk is an opaque data type. (You don't really
  1408.  *  want to know what's going on inside do you?)
  1409.  */
  1410.  
  1411. /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
  1412.  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
  1413.  *  atom. (They are also useful for lists which use MemChunk to allocate
  1414.  *  memory but are also part of the MemChunk implementation).
  1415.  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
  1416.  */
  1417.  
  1418. #define G_ALLOC_ONLY      1
  1419. #define G_ALLOC_AND_FREE  2
  1420.  
  1421. GMemChunk* g_mem_chunk_new     (gchar      *name,
  1422.                 gint       atom_size,
  1423.                 gulong       area_size,
  1424.                 gint       type);
  1425. void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
  1426. gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
  1427. gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
  1428. void       g_mem_chunk_free    (GMemChunk *mem_chunk,
  1429.                 gpointer   mem);
  1430. void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
  1431. void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
  1432. void       g_mem_chunk_print   (GMemChunk *mem_chunk);
  1433. void       g_mem_chunk_info    (void);
  1434.  
  1435. /* Ah yes...we have a "g_blow_chunks" function.
  1436.  * "g_blow_chunks" simply compresses all the chunks. This operation
  1437.  *  consists of freeing every memory area that should be freed (but
  1438.  *  which we haven't gotten around to doing yet). And, no,
  1439.  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
  1440.  *  much better name than "g_mem_chunk_clean_all" or something
  1441.  *  similar.
  1442.  */
  1443. void g_blow_chunks (void);
  1444.  
  1445.  
  1446. /* Timer
  1447.  */
  1448. GTimer* g_timer_new    (void);
  1449. void    g_timer_destroy (GTimer     *timer);
  1450. void    g_timer_start    (GTimer     *timer);
  1451. void    g_timer_stop    (GTimer     *timer);
  1452. void    g_timer_reset    (GTimer     *timer);
  1453. gdouble g_timer_elapsed (GTimer     *timer,
  1454.              gulong     *microseconds);
  1455.  
  1456.  
  1457. /* String utility functions that modify a string argument or
  1458.  * return a constant string that must not be freed.
  1459.  */
  1460. #define     G_STR_DELIMITERS    "_-|> <."
  1461. gchar*     g_strdelimit        (gchar         *string,
  1462.                  const gchar *delimiters,
  1463.                  gchar          new_delimiter);
  1464. gdouble     g_strtod        (const gchar *nptr,
  1465.                  gchar        **endptr);
  1466. gchar*     g_strerror        (gint          errnum);
  1467. gchar*     g_strsignal        (gint          signum);
  1468. gint     g_strcasecmp        (const gchar *s1,
  1469.                  const gchar *s2);
  1470. gint     g_strncasecmp        (const gchar *s1,
  1471.                  const gchar *s2,
  1472.                  guint           n);
  1473. void     g_strdown        (gchar         *string);
  1474. void     g_strup        (gchar         *string);
  1475. void     g_strreverse        (gchar         *string);
  1476. /* removes leading spaces */
  1477. gchar*   g_strchug              (gchar        *string);
  1478. /* removes trailing spaces */
  1479. gchar*  g_strchomp              (gchar        *string);
  1480. /* removes leading & trailing spaces */
  1481. #define g_strstrip( string )    g_strchomp (g_strchug (string))
  1482.  
  1483. /* String utility functions that return a newly allocated string which
  1484.  * ought to be freed from the caller at some point.
  1485.  */
  1486. gchar*     g_strdup        (const gchar *str);
  1487. gchar*     g_strdup_printf    (const gchar *format,
  1488.                  ...) G_GNUC_PRINTF (1, 2);
  1489. gchar*     g_strdup_vprintf    (const gchar *format,
  1490.                  va_list      args);
  1491. gchar*     g_strndup        (const gchar *str,
  1492.                  guint          n);
  1493. gchar*     g_strnfill        (guint          length,
  1494.                  gchar          fill_char);
  1495. gchar*     g_strconcat        (const gchar *string1,
  1496.                  ...); /* NULL terminated */
  1497. gchar*   g_strjoin        (const gchar  *separator,
  1498.                  ...); /* NULL terminated */
  1499. gchar*     g_strescape        (gchar          *string);
  1500. gpointer g_memdup        (gconstpointer mem,
  1501.                  guint           byte_size);
  1502.  
  1503. /* NULL terminated string arrays.
  1504.  * g_strsplit() splits up string into max_tokens tokens at delim and
  1505.  * returns a newly allocated string array.
  1506.  * g_strjoinv() concatenates all of str_array's strings, sliding in an
  1507.  * optional separator, the returned string is newly allocated.
  1508.  * g_strfreev() frees the array itself and all of its strings.
  1509.  */
  1510. gchar**     g_strsplit        (const gchar  *string,
  1511.                  const gchar  *delimiter,
  1512.                  gint          max_tokens);
  1513. gchar*   g_strjoinv        (const gchar  *separator,
  1514.                  gchar       **str_array);
  1515. void     g_strfreev        (gchar       **str_array);
  1516.  
  1517.  
  1518.  
  1519. /* calculate a string size, guarranteed to fit format + args.
  1520.  */
  1521. guint    g_printf_string_upper_bound (const gchar* format,
  1522.                      va_list      args);
  1523.  
  1524.  
  1525. /* Retrive static string info
  1526.  */
  1527. gchar*    g_get_user_name        (void);
  1528. gchar*    g_get_real_name        (void);
  1529. gchar*    g_get_home_dir        (void);
  1530. gchar*    g_get_tmp_dir        (void);
  1531. gchar*    g_get_prgname        (void);
  1532. void    g_set_prgname        (const gchar *prgname);
  1533.  
  1534.  
  1535. /* Miscellaneous utility functions
  1536.  */
  1537. guint    g_parse_debug_string    (const gchar *string,
  1538.                  GDebugKey   *keys,
  1539.                  guint          nkeys);
  1540. gint    g_snprintf        (gchar         *string,
  1541.                  gulong          n,
  1542.                  gchar const *format,
  1543.                  ...) G_GNUC_PRINTF (3, 4);
  1544. gint    g_vsnprintf        (gchar         *string,
  1545.                  gulong          n,
  1546.                  gchar const *format,
  1547.                  va_list      args);
  1548. gchar*    g_basename        (const gchar *file_name);
  1549. /* Check if a file name is an absolute path */
  1550. gboolean g_path_is_absolute    (const gchar *file_name);
  1551. /* In case of absolute paths, skip the root part */
  1552. gchar*  g_path_skip_root    (gchar       *file_name);
  1553.  
  1554. /* strings are newly allocated with g_malloc() */
  1555. gchar*    g_dirname        (const gchar *file_name);
  1556. gchar*    g_get_current_dir    (void);
  1557.  
  1558. /* return the environment string for the variable. The returned memory
  1559.  * must not be freed. */
  1560. gchar*  g_getenv        (const gchar *variable);
  1561.  
  1562.  
  1563. /* we use a GLib function as a replacement for ATEXIT, so
  1564.  * the programmer is not required to check the return value
  1565.  * (if there is any in the implementation) and doesn't encounter
  1566.  * missing include files.
  1567.  */
  1568. void    g_atexit        (GVoidFunc    func);
  1569.  
  1570.  
  1571. /* Bit tests
  1572.  */
  1573. G_INLINE_FUNC gint    g_bit_nth_lsf (guint32 mask,
  1574.                        gint    nth_bit);
  1575. #ifdef    G_CAN_INLINE
  1576. G_INLINE_FUNC gint
  1577. g_bit_nth_lsf (guint32 mask,
  1578.            gint    nth_bit)
  1579. {
  1580.   do
  1581.     {
  1582.       nth_bit++;
  1583.       if (mask & (1 << (guint) nth_bit))
  1584.     return nth_bit;
  1585.     }
  1586.   while (nth_bit < 32);
  1587.   return -1;
  1588. }
  1589. #endif    /* G_CAN_INLINE */
  1590.  
  1591. G_INLINE_FUNC gint    g_bit_nth_msf (guint32 mask,
  1592.                        gint    nth_bit);
  1593. #ifdef G_CAN_INLINE
  1594. G_INLINE_FUNC gint
  1595. g_bit_nth_msf (guint32 mask,
  1596.            gint    nth_bit)
  1597. {
  1598.   if (nth_bit < 0)
  1599.     nth_bit = 32;
  1600.   do
  1601.     {
  1602.       nth_bit--;
  1603.       if (mask & (1 << (guint) nth_bit))
  1604.     return nth_bit;
  1605.     }
  1606.   while (nth_bit > 0);
  1607.   return -1;
  1608. }
  1609. #endif    /* G_CAN_INLINE */
  1610.  
  1611. G_INLINE_FUNC guint    g_bit_storage (guint number);
  1612. #ifdef G_CAN_INLINE
  1613. G_INLINE_FUNC guint
  1614. g_bit_storage (guint number)
  1615. {
  1616.   register guint n_bits = 0;
  1617.   
  1618.   do
  1619.     {
  1620.       n_bits++;
  1621.       number >>= 1;
  1622.     }
  1623.   while (number);
  1624.   return n_bits;
  1625. }
  1626. #endif    /* G_CAN_INLINE */
  1627.  
  1628. /* String Chunks
  1629.  */
  1630. GStringChunk* g_string_chunk_new       (gint size);
  1631. void          g_string_chunk_free       (GStringChunk *chunk);
  1632. gchar*          g_string_chunk_insert       (GStringChunk *chunk,
  1633.                         const gchar     *string);
  1634. gchar*          g_string_chunk_insert_const  (GStringChunk *chunk,
  1635.                         const gchar     *string);
  1636.  
  1637.  
  1638. /* Strings
  1639.  */
  1640. GString* g_string_new        (const gchar *init);
  1641. GString* g_string_sized_new (guint      dfl_size);
  1642. void     g_string_free        (GString     *string,
  1643.                  gint      free_segment);
  1644. GString* g_string_assign    (GString     *lval,
  1645.                  const gchar *rval);
  1646. GString* g_string_truncate  (GString     *string,
  1647.                  gint      len);
  1648. GString* g_string_append    (GString     *string,
  1649.                  const gchar *val);
  1650. GString* g_string_append_c  (GString     *string,
  1651.                  gchar      c);
  1652. GString* g_string_prepend   (GString     *string,
  1653.                  const gchar *val);
  1654. GString* g_string_prepend_c (GString     *string,
  1655.                  gchar      c);
  1656. GString* g_string_insert    (GString     *string,
  1657.                  gint      pos,
  1658.                  const gchar *val);
  1659. GString* g_string_insert_c  (GString     *string,
  1660.                  gint      pos,
  1661.                  gchar      c);
  1662. GString* g_string_erase        (GString     *string,
  1663.                  gint      pos,
  1664.                  gint      len);
  1665. GString* g_string_down        (GString     *string);
  1666. GString* g_string_up        (GString     *string);
  1667. void     g_string_sprintf   (GString     *string,
  1668.                  const gchar *format,
  1669.                  ...) G_GNUC_PRINTF (2, 3);
  1670. void     g_string_sprintfa  (GString     *string,
  1671.                  const gchar *format,
  1672.                  ...) G_GNUC_PRINTF (2, 3);
  1673.  
  1674.  
  1675. /* Resizable arrays, remove fills any cleared spot and shortens the
  1676.  * array, while preserving the order. remove_fast will distort the
  1677.  * order by moving the last element to the position of the removed 
  1678.  */
  1679.  
  1680. #define g_array_append_val(a,v)      g_array_append_vals (a, &(v), 1)
  1681. #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &(v), 1)
  1682. #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &(v), 1)
  1683. #define g_array_index(a,t,i)      (((t*) (a)->data) [(i)])
  1684.  
  1685. GArray* g_array_new              (gboolean        zero_terminated,
  1686.                    gboolean        clear,
  1687.                    guint        element_size);
  1688. void    g_array_free              (GArray       *array,
  1689.                    gboolean        free_segment);
  1690. GArray* g_array_append_vals       (GArray       *array,
  1691.                    gconstpointer    data,
  1692.                    guint        len);
  1693. GArray* g_array_prepend_vals      (GArray       *array,
  1694.                    gconstpointer    data,
  1695.                    guint        len);
  1696. GArray* g_array_insert_vals       (GArray          *array,
  1697.                    guint            index,
  1698.                    gconstpointer    data,
  1699.                    guint            len);
  1700. GArray* g_array_set_size          (GArray       *array,
  1701.                    guint        length);
  1702. GArray* g_array_remove_index      (GArray       *array,
  1703.                    guint        index);
  1704. GArray* g_array_remove_index_fast (GArray       *array,
  1705.                    guint        index);
  1706.  
  1707. /* Resizable pointer array.  This interface is much less complicated
  1708.  * than the above.  Add appends appends a pointer.  Remove fills any
  1709.  * cleared spot and shortens the array. remove_fast will again distort
  1710.  * order.  
  1711.  */
  1712. #define        g_ptr_array_index(array,index) (array->pdata)[index]
  1713. GPtrArray*  g_ptr_array_new           (void);
  1714. void        g_ptr_array_free           (GPtrArray    *array,
  1715.                         gboolean     free_seg);
  1716. void        g_ptr_array_set_size       (GPtrArray    *array,
  1717.                         gint     length);
  1718. gpointer    g_ptr_array_remove_index       (GPtrArray    *array,
  1719.                         guint     index);
  1720. gpointer    g_ptr_array_remove_index_fast  (GPtrArray    *array,
  1721.                         guint     index);
  1722. gboolean    g_ptr_array_remove           (GPtrArray    *array,
  1723.                         gpointer     data);
  1724. gboolean    g_ptr_array_remove_fast        (GPtrArray    *array,
  1725.                         gpointer     data);
  1726. void        g_ptr_array_add           (GPtrArray    *array,
  1727.                         gpointer     data);
  1728.  
  1729. /* Byte arrays, an array of guint8.  Implemented as a GArray,
  1730.  * but type-safe.
  1731.  */
  1732.  
  1733. GByteArray* g_byte_array_new               (void);
  1734. void        g_byte_array_free               (GByteArray     *array,
  1735.                         gboolean      free_segment);
  1736. GByteArray* g_byte_array_append               (GByteArray     *array,
  1737.                         const guint8 *data,
  1738.                         guint      len);
  1739. GByteArray* g_byte_array_prepend           (GByteArray     *array,
  1740.                         const guint8 *data,
  1741.                         guint      len);
  1742. GByteArray* g_byte_array_set_size          (GByteArray     *array,
  1743.                         guint      length);
  1744. GByteArray* g_byte_array_remove_index       (GByteArray     *array,
  1745.                         guint      index);
  1746. GByteArray* g_byte_array_remove_index_fast (GByteArray     *array,
  1747.                         guint      index);
  1748.  
  1749.  
  1750. /* Hash Functions
  1751.  */
  1752. gint  g_str_equal (gconstpointer   v,
  1753.            gconstpointer   v2);
  1754. guint g_str_hash  (gconstpointer   v);
  1755.  
  1756. gint  g_int_equal (gconstpointer   v,
  1757.            gconstpointer   v2);
  1758. guint g_int_hash  (gconstpointer   v);
  1759.  
  1760. /* This "hash" function will just return the key's adress as an
  1761.  * unsigned integer. Useful for hashing on plain adresses or
  1762.  * simple integer values.
  1763.  * passing NULL into g_hash_table_new() as GHashFunc has the
  1764.  * same effect as passing g_direct_hash().
  1765.  */
  1766. guint g_direct_hash  (gconstpointer v);
  1767. gint  g_direct_equal (gconstpointer v,
  1768.               gconstpointer v2);
  1769.  
  1770.  
  1771. /* Quarks (string<->id association)
  1772.  */
  1773. GQuark      g_quark_try_string        (const gchar    *string);
  1774. GQuark      g_quark_from_static_string    (const gchar    *string);
  1775. GQuark      g_quark_from_string        (const gchar    *string);
  1776. gchar*      g_quark_to_string        (GQuark         quark);
  1777.  
  1778.  
  1779. /* Keyed Data List
  1780.  * NOTE: these functions are scheduled for a rename in GLib 1.3
  1781.  */
  1782. void      g_datalist_init         (GData         **datalist);
  1783. void      g_datalist_clear         (GData         **datalist);
  1784. gpointer  g_datalist_id_get_data     (GData         **datalist,
  1785.                       GQuark       key_id);
  1786. void      g_datalist_id_set_data_full     (GData         **datalist,
  1787.                       GQuark       key_id,
  1788.                       gpointer       data,
  1789.                       GDestroyNotify   destroy_func);
  1790. void      g_datalist_id_remove_no_notify (GData         **datalist,
  1791.                       GQuark       key_id);
  1792. void      g_datalist_foreach         (GData         **datalist,
  1793.                       GDataForeachFunc func,
  1794.                       gpointer       user_data);
  1795. #define      g_datalist_id_set_data(dl, q, d)    \
  1796.      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
  1797. #define      g_datalist_id_remove_data(dl, q)    \
  1798.      g_datalist_id_set_data ((dl), (q), NULL)
  1799. #define      g_datalist_get_data(dl, k)        \
  1800.      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
  1801. #define      g_datalist_set_data_full(dl, k, d, f)    \
  1802.      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
  1803. #define      g_datalist_remove_no_notify(dl, k)    \
  1804.      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
  1805. #define      g_datalist_set_data(dl, k, d)        \
  1806.      g_datalist_set_data_full ((dl), (k), (d), NULL)
  1807. #define      g_datalist_remove_data(dl, k)        \
  1808.      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
  1809.  
  1810.  
  1811. /* Location Associated Keyed Data
  1812.  * NOTE: these functions are scheduled for a rename in GLib 1.3
  1813.  */
  1814. void      g_dataset_destroy        (gconstpointer      dataset_location);
  1815. gpointer  g_dataset_id_get_data        (gconstpointer      dataset_location,
  1816.                      GQuark          key_id);
  1817. void      g_dataset_id_set_data_full    (gconstpointer      dataset_location,
  1818.                      GQuark          key_id,
  1819.                      gpointer      data,
  1820.                      GDestroyNotify      destroy_func);
  1821. void      g_dataset_id_remove_no_notify    (gconstpointer      dataset_location,
  1822.                      GQuark          key_id);
  1823. void      g_dataset_foreach        (gconstpointer      dataset_location,
  1824.                      GDataForeachFunc func,
  1825.                      gpointer      user_data);
  1826. #define      g_dataset_id_set_data(l, k, d)    \
  1827.      g_dataset_id_set_data_full ((l), (k), (d), NULL)
  1828. #define      g_dataset_id_remove_data(l, k)    \
  1829.      g_dataset_id_set_data ((l), (k), NULL)
  1830. #define      g_dataset_get_data(l, k)        \
  1831.      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
  1832. #define      g_dataset_set_data_full(l, k, d, f)    \
  1833.      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
  1834. #define      g_dataset_remove_no_notify(l, k)    \
  1835.      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
  1836. #define      g_dataset_set_data(l, k, d)        \
  1837.      g_dataset_set_data_full ((l), (k), (d), NULL)
  1838. #define      g_dataset_remove_data(l, k)        \
  1839.      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
  1840.  
  1841.  
  1842. /* GScanner: Flexible lexical scanner for general purpose.
  1843.  */
  1844.  
  1845. /* Character sets */
  1846. #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1847. #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
  1848. #define G_CSET_LATINC    "\300\301\302\303\304\305\306"\
  1849.             "\307\310\311\312\313\314\315\316\317\320"\
  1850.             "\321\322\323\324\325\326"\
  1851.             "\330\331\332\333\334\335\336"
  1852. #define G_CSET_LATINS    "\337\340\341\342\343\344\345\346"\
  1853.             "\347\350\351\352\353\354\355\356\357\360"\
  1854.             "\361\362\363\364\365\366"\
  1855.             "\370\371\372\373\374\375\376\377"
  1856.  
  1857. /* Error types */
  1858. typedef enum
  1859. {
  1860.   G_ERR_UNKNOWN,
  1861.   G_ERR_UNEXP_EOF,
  1862.   G_ERR_UNEXP_EOF_IN_STRING,
  1863.   G_ERR_UNEXP_EOF_IN_COMMENT,
  1864.   G_ERR_NON_DIGIT_IN_CONST,
  1865.   G_ERR_DIGIT_RADIX,
  1866.   G_ERR_FLOAT_RADIX,
  1867.   G_ERR_FLOAT_MALFORMED
  1868. } GErrorType;
  1869.  
  1870. /* Token types */
  1871. typedef enum
  1872. {
  1873.   G_TOKEN_EOF            =   0,
  1874.   
  1875.   G_TOKEN_LEFT_PAREN        = '(',
  1876.   G_TOKEN_RIGHT_PAREN        = ')',
  1877.   G_TOKEN_LEFT_CURLY        = '{',
  1878.   G_TOKEN_RIGHT_CURLY        = '}',
  1879.   G_TOKEN_LEFT_BRACE        = '[',
  1880.   G_TOKEN_RIGHT_BRACE        = ']',
  1881.   G_TOKEN_EQUAL_SIGN        = '=',
  1882.   G_TOKEN_COMMA            = ',',
  1883.   
  1884.   G_TOKEN_NONE            = 256,
  1885.   
  1886.   G_TOKEN_ERROR,
  1887.   
  1888.   G_TOKEN_CHAR,
  1889.   G_TOKEN_BINARY,
  1890.   G_TOKEN_OCTAL,
  1891.   G_TOKEN_INT,
  1892.   G_TOKEN_HEX,
  1893.   G_TOKEN_FLOAT,
  1894.   G_TOKEN_STRING,
  1895.   
  1896.   G_TOKEN_SYMBOL,
  1897.   G_TOKEN_IDENTIFIER,
  1898.   G_TOKEN_IDENTIFIER_NULL,
  1899.   
  1900.   G_TOKEN_COMMENT_SINGLE,
  1901.   G_TOKEN_COMMENT_MULTI,
  1902.   G_TOKEN_LAST
  1903. } GTokenType;
  1904.  
  1905. union    _GTokenValue
  1906. {
  1907.   gpointer    v_symbol;
  1908.   gchar        *v_identifier;
  1909.   gulong    v_binary;
  1910.   gulong    v_octal;
  1911.   gulong    v_int;
  1912.   gdouble    v_float;
  1913.   gulong    v_hex;
  1914.   gchar        *v_string;
  1915.   gchar        *v_comment;
  1916.   guchar    v_char;
  1917.   guint        v_error;
  1918. };
  1919.  
  1920. struct    _GScannerConfig
  1921. {
  1922.   /* Character sets
  1923.    */
  1924.   gchar        *cset_skip_characters;        /* default: " \t\n" */
  1925.   gchar        *cset_identifier_first;
  1926.   gchar        *cset_identifier_nth;
  1927.   gchar        *cpair_comment_single;        /* default: "#\n" */
  1928.   
  1929.   /* Should symbol lookup work case sensitive?
  1930.    */
  1931.   guint        case_sensitive : 1;
  1932.   
  1933.   /* Boolean values to be adjusted "on the fly"
  1934.    * to configure scanning behaviour.
  1935.    */
  1936.   guint        skip_comment_multi : 1;        /* C like comment */
  1937.   guint        skip_comment_single : 1;    /* single line comment */
  1938.   guint        scan_comment_multi : 1;        /* scan multi line comments? */
  1939.   guint        scan_identifier : 1;
  1940.   guint        scan_identifier_1char : 1;
  1941.   guint        scan_identifier_NULL : 1;
  1942.   guint        scan_symbols : 1;
  1943.   guint        scan_binary : 1;
  1944.   guint        scan_octal : 1;
  1945.   guint        scan_float : 1;
  1946.   guint        scan_hex : 1;            /* `0x0ff0' */
  1947.   guint        scan_hex_dollar : 1;        /* `$0ff0' */
  1948.   guint        scan_string_sq : 1;        /* string: 'anything' */
  1949.   guint        scan_string_dq : 1;        /* string: "\\-escapes!\n" */
  1950.   guint        numbers_2_int : 1;        /* bin, octal, hex => int */
  1951.   guint        int_2_float : 1;        /* int => G_TOKEN_FLOAT? */
  1952.   guint        identifier_2_string : 1;
  1953.   guint        char_2_token : 1;        /* return G_TOKEN_CHAR? */
  1954.   guint        symbol_2_token : 1;
  1955.   guint        scope_0_fallback : 1;        /* try scope 0 on lookups? */
  1956. };
  1957.  
  1958. struct    _GScanner
  1959. {
  1960.   /* unused fields */
  1961.   gpointer        user_data;
  1962.   guint            max_parse_errors;
  1963.   
  1964.   /* g_scanner_error() increments this field */
  1965.   guint            parse_errors;
  1966.   
  1967.   /* name of input stream, featured by the default message handler */
  1968.   const gchar        *input_name;
  1969.   
  1970.   /* data pointer for derived structures */
  1971.   gpointer        derived_data;
  1972.   
  1973.   /* link into the scanner configuration */
  1974.   GScannerConfig    *config;
  1975.   
  1976.   /* fields filled in after g_scanner_get_next_token() */
  1977.   GTokenType        token;
  1978.   GTokenValue        value;
  1979.   guint            line;
  1980.   guint            position;
  1981.   
  1982.   /* fields filled in after g_scanner_peek_next_token() */
  1983.   GTokenType        next_token;
  1984.   GTokenValue        next_value;
  1985.   guint            next_line;
  1986.   guint            next_position;
  1987.   
  1988.   /* to be considered private */
  1989.   GHashTable        *symbol_table;
  1990.   gint            input_fd;
  1991.   const gchar        *text;
  1992.   const gchar        *text_end;
  1993.   gchar            *buffer;
  1994.   guint            scope_id;
  1995.   
  1996.   /* handler function for _warn and _error */
  1997.   GScannerMsgFunc    msg_handler;
  1998. };
  1999.  
  2000. GScanner*    g_scanner_new            (GScannerConfig *config_templ);
  2001. void        g_scanner_destroy        (GScanner    *scanner);
  2002. void        g_scanner_input_file        (GScanner    *scanner,
  2003.                          gint        input_fd);
  2004. void        g_scanner_sync_file_offset    (GScanner    *scanner);
  2005. void        g_scanner_input_text        (GScanner    *scanner,
  2006.                          const    gchar    *text,
  2007.                          guint        text_len);
  2008. GTokenType    g_scanner_get_next_token    (GScanner    *scanner);
  2009. GTokenType    g_scanner_peek_next_token    (GScanner    *scanner);
  2010. GTokenType    g_scanner_cur_token        (GScanner    *scanner);
  2011. GTokenValue    g_scanner_cur_value        (GScanner    *scanner);
  2012. guint        g_scanner_cur_line        (GScanner    *scanner);
  2013. guint        g_scanner_cur_position        (GScanner    *scanner);
  2014. gboolean    g_scanner_eof            (GScanner    *scanner);
  2015. guint        g_scanner_set_scope        (GScanner    *scanner,
  2016.                          guint         scope_id);
  2017. void        g_scanner_scope_add_symbol    (GScanner    *scanner,
  2018.                          guint         scope_id,
  2019.                          const gchar    *symbol,
  2020.                          gpointer    value);
  2021. void        g_scanner_scope_remove_symbol    (GScanner    *scanner,
  2022.                          guint         scope_id,
  2023.                          const gchar    *symbol);
  2024. gpointer    g_scanner_scope_lookup_symbol    (GScanner    *scanner,
  2025.                          guint         scope_id,
  2026.                          const gchar    *symbol);
  2027. void        g_scanner_scope_foreach_symbol    (GScanner    *scanner,
  2028.                          guint         scope_id,
  2029.                          GHFunc         func,
  2030.                          gpointer     user_data);
  2031. gpointer    g_scanner_lookup_symbol        (GScanner    *scanner,
  2032.                          const gchar    *symbol);
  2033. void        g_scanner_freeze_symbol_table    (GScanner    *scanner);
  2034. void        g_scanner_thaw_symbol_table    (GScanner    *scanner);
  2035. void        g_scanner_unexp_token        (GScanner    *scanner,
  2036.                          GTokenType    expected_token,
  2037.                          const gchar    *identifier_spec,
  2038.                          const gchar    *symbol_spec,
  2039.                          const gchar    *symbol_name,
  2040.                          const gchar    *message,
  2041.                          gint         is_error);
  2042. void        g_scanner_error            (GScanner    *scanner,
  2043.                          const gchar    *format,
  2044.                          ...) G_GNUC_PRINTF (2,3);
  2045. void        g_scanner_warn            (GScanner    *scanner,
  2046.                          const gchar    *format,
  2047.                          ...) G_GNUC_PRINTF (2,3);
  2048. gint        g_scanner_stat_mode        (const gchar    *filename);
  2049. /* keep downward source compatibility */
  2050. #define        g_scanner_add_symbol( scanner, symbol, value )    G_STMT_START { \
  2051.   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
  2052. } G_STMT_END
  2053. #define        g_scanner_remove_symbol( scanner, symbol )    G_STMT_START { \
  2054.   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
  2055. } G_STMT_END
  2056. #define        g_scanner_foreach_symbol( scanner, func, data )    G_STMT_START { \
  2057.   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
  2058. } G_STMT_END
  2059.  
  2060.  
  2061. /* GCompletion
  2062.  */
  2063.  
  2064. struct _GCompletion
  2065. {
  2066.   GList* items;
  2067.   GCompletionFunc func;
  2068.   
  2069.   gchar* prefix;
  2070.   GList* cache;
  2071. };
  2072.  
  2073. GCompletion* g_completion_new           (GCompletionFunc func);
  2074. void         g_completion_add_items    (GCompletion*    cmp,
  2075.                     GList*        items);
  2076. void         g_completion_remove_items (GCompletion*    cmp,
  2077.                     GList*        items);
  2078. void         g_completion_clear_items  (GCompletion*    cmp);
  2079. GList*         g_completion_complete     (GCompletion*    cmp,
  2080.                     gchar*        prefix,
  2081.                     gchar**        new_prefix);
  2082. void         g_completion_free           (GCompletion*    cmp);
  2083.  
  2084.  
  2085. /* GDate
  2086.  *
  2087.  * Date calculations (not time for now, to be resolved). These are a
  2088.  * mutant combination of Steffen Beyer's DateCalc routines
  2089.  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
  2090.  * date routines (written for in-house software).  Written by Havoc
  2091.  * Pennington <hp@pobox.com> 
  2092.  */
  2093.  
  2094. typedef guint16 GDateYear;
  2095. typedef guint8  GDateDay;   /* day of the month */
  2096. typedef struct _GDate GDate;
  2097. /* make struct tm known without having to include time.h */
  2098. struct tm;
  2099.  
  2100. /* enum used to specify order of appearance in parsed date strings */
  2101. typedef enum
  2102. {
  2103.   G_DATE_DAY   = 0,
  2104.   G_DATE_MONTH = 1,
  2105.   G_DATE_YEAR  = 2
  2106. } GDateDMY;
  2107.  
  2108. /* actual week and month values */
  2109. typedef enum
  2110. {
  2111.   G_DATE_BAD_WEEKDAY  = 0,
  2112.   G_DATE_MONDAY       = 1,
  2113.   G_DATE_TUESDAY      = 2,
  2114.   G_DATE_WEDNESDAY    = 3,
  2115.   G_DATE_THURSDAY     = 4,
  2116.   G_DATE_FRIDAY       = 5,
  2117.   G_DATE_SATURDAY     = 6,
  2118.   G_DATE_SUNDAY       = 7
  2119. } GDateWeekday;
  2120. typedef enum
  2121. {
  2122.   G_DATE_BAD_MONTH = 0,
  2123.   G_DATE_JANUARY   = 1,
  2124.   G_DATE_FEBRUARY  = 2,
  2125.   G_DATE_MARCH     = 3,
  2126.   G_DATE_APRIL     = 4,
  2127.   G_DATE_MAY       = 5,
  2128.   G_DATE_JUNE      = 6,
  2129.   G_DATE_JULY      = 7,
  2130.   G_DATE_AUGUST    = 8,
  2131.   G_DATE_SEPTEMBER = 9,
  2132.   G_DATE_OCTOBER   = 10,
  2133.   G_DATE_NOVEMBER  = 11,
  2134.   G_DATE_DECEMBER  = 12
  2135. } GDateMonth;
  2136.  
  2137. #define G_DATE_BAD_JULIAN 0U
  2138. #define G_DATE_BAD_DAY    0U
  2139. #define G_DATE_BAD_YEAR   0U
  2140.  
  2141. /* Note: directly manipulating structs is generally a bad idea, but
  2142.  * in this case it's an *incredibly* bad idea, because all or part
  2143.  * of this struct can be invalid at any given time. Use the functions,
  2144.  * or you will get hosed, I promise.
  2145.  */
  2146. struct _GDate
  2147.   guint julian_days : 32; /* julian days representation - we use a
  2148.                            *  bitfield hoping that 64 bit platforms
  2149.                            *  will pack this whole struct in one big
  2150.                            *  int 
  2151.                            */
  2152.  
  2153.   guint julian : 1;    /* julian is valid */
  2154.   guint dmy    : 1;    /* dmy is valid */
  2155.  
  2156.   /* DMY representation */
  2157.   guint day    : 6;  
  2158.   guint month  : 4; 
  2159.   guint year   : 16; 
  2160. };
  2161.  
  2162. /* g_date_new() returns an invalid date, you then have to _set() stuff 
  2163.  * to get a usable object. You can also allocate a GDate statically,
  2164.  * then call g_date_clear() to initialize.
  2165.  */
  2166. GDate*       g_date_new                   (void);
  2167. GDate*       g_date_new_dmy               (GDateDay     day, 
  2168.                                            GDateMonth   month, 
  2169.                                            GDateYear    year);
  2170. GDate*       g_date_new_julian            (guint32      julian_day);
  2171. void         g_date_free                  (GDate       *date);
  2172.  
  2173. /* check g_date_valid() after doing an operation that might fail, like
  2174.  * _parse.  Almost all g_date operations are undefined on invalid
  2175.  * dates (the exceptions are the mutators, since you need those to
  2176.  * return to validity).  
  2177.  */
  2178. gboolean     g_date_valid                 (GDate       *date);
  2179. gboolean     g_date_valid_day             (GDateDay     day);
  2180. gboolean     g_date_valid_month           (GDateMonth   month);
  2181. gboolean     g_date_valid_year            (GDateYear    year);
  2182. gboolean     g_date_valid_weekday         (GDateWeekday weekday);
  2183. gboolean     g_date_valid_julian          (guint32      julian_date);
  2184. gboolean     g_date_valid_dmy             (GDateDay     day,
  2185.                                            GDateMonth   month,
  2186.                                            GDateYear    year);
  2187.  
  2188. GDateWeekday g_date_weekday               (GDate       *date);
  2189. GDateMonth   g_date_month                 (GDate       *date);
  2190. GDateYear    g_date_year                  (GDate       *date);
  2191. GDateDay     g_date_day                   (GDate       *date);
  2192. guint32      g_date_julian                (GDate       *date);
  2193. guint        g_date_day_of_year           (GDate       *date);
  2194.  
  2195. /* First monday/sunday is the start of week 1; if we haven't reached
  2196.  * that day, return 0. These are not ISO weeks of the year; that
  2197.  * routine needs to be added.
  2198.  * these functions return the number of weeks, starting on the
  2199.  * corrsponding day
  2200.  */
  2201. guint        g_date_monday_week_of_year   (GDate      *date);
  2202. guint        g_date_sunday_week_of_year   (GDate      *date);
  2203.  
  2204. /* If you create a static date struct you need to clear it to get it
  2205.  * in a sane state before use. You can clear a whole array at
  2206.  * once with the ndates argument.
  2207.  */
  2208. void         g_date_clear                 (GDate       *date, 
  2209.                                            guint        n_dates);
  2210.  
  2211. /* The parse routine is meant for dates typed in by a user, so it
  2212.  * permits many formats but tries to catch common typos. If your data
  2213.  * needs to be strictly validated, it is not an appropriate function.
  2214.  */
  2215. void         g_date_set_parse             (GDate       *date,
  2216.                                            const gchar *str);
  2217. void         g_date_set_time              (GDate       *date, 
  2218.                                            GTime        time);
  2219. void         g_date_set_month             (GDate       *date, 
  2220.                                            GDateMonth   month);
  2221. void         g_date_set_day               (GDate       *date, 
  2222.                                            GDateDay     day);
  2223. void         g_date_set_year              (GDate       *date,
  2224.                                            GDateYear    year);
  2225. void         g_date_set_dmy               (GDate       *date,
  2226.                                            GDateDay     day,
  2227.                                            GDateMonth   month,
  2228.                                            GDateYear    y);
  2229. void         g_date_set_julian            (GDate       *date,
  2230.                                            guint32      julian_date);
  2231. gboolean     g_date_is_first_of_month     (GDate       *date);
  2232. gboolean     g_date_is_last_of_month      (GDate       *date);
  2233.  
  2234. /* To go forward by some number of weeks just go forward weeks*7 days */
  2235. void         g_date_add_days              (GDate       *date, 
  2236.                                            guint        n_days);
  2237. void         g_date_subtract_days         (GDate       *date, 
  2238.                                            guint        n_days);
  2239.  
  2240. /* If you add/sub months while day > 28, the day might change */
  2241. void         g_date_add_months            (GDate       *date,
  2242.                                            guint        n_months);
  2243. void         g_date_subtract_months       (GDate       *date,
  2244.                                            guint        n_months);
  2245.  
  2246. /* If it's feb 29, changing years can move you to the 28th */
  2247. void         g_date_add_years             (GDate       *date,
  2248.                                            guint        n_years);
  2249. void         g_date_subtract_years        (GDate       *date,
  2250.                                            guint        n_years);
  2251. gboolean     g_date_is_leap_year          (GDateYear    year);
  2252. guint8       g_date_days_in_month         (GDateMonth   month, 
  2253.                                            GDateYear    year);
  2254. guint8       g_date_monday_weeks_in_year  (GDateYear    year);
  2255. guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
  2256.  
  2257. /* qsort-friendly (with a cast...) */
  2258. gint         g_date_compare               (GDate       *lhs,
  2259.                                            GDate       *rhs);
  2260. void         g_date_to_struct_tm          (GDate       *date,
  2261.                                            struct tm   *tm);
  2262.  
  2263. /* Just like strftime() except you can only use date-related formats.
  2264.  *   Using a time format is undefined.
  2265.  */
  2266. gsize        g_date_strftime              (gchar       *s,
  2267.                                            gsize        slen,
  2268.                                            const gchar *format,
  2269.                                            GDate       *date);
  2270.  
  2271.  
  2272. /* GRelation
  2273.  *
  2274.  * Indexed Relations.  Imagine a really simple table in a
  2275.  * database.  Relations are not ordered.  This data type is meant for
  2276.  * maintaining a N-way mapping.
  2277.  *
  2278.  * g_relation_new() creates a relation with FIELDS fields
  2279.  *
  2280.  * g_relation_destroy() frees all resources
  2281.  * g_tuples_destroy() frees the result of g_relation_select()
  2282.  *
  2283.  * g_relation_index() indexes relation FIELD with the provided
  2284.  *   equality and hash functions.  this must be done before any
  2285.  *   calls to insert are made.
  2286.  *
  2287.  * g_relation_insert() inserts a new tuple.  you are expected to
  2288.  *   provide the right number of fields.
  2289.  *
  2290.  * g_relation_delete() deletes all relations with KEY in FIELD
  2291.  * g_relation_select() returns ...
  2292.  * g_relation_count() counts ...
  2293.  */
  2294.  
  2295. GRelation* g_relation_new     (gint        fields);
  2296. void       g_relation_destroy (GRelation   *relation);
  2297. void       g_relation_index   (GRelation   *relation,
  2298.                    gint        field,
  2299.                    GHashFunc    hash_func,
  2300.                    GCompareFunc key_compare_func);
  2301. void       g_relation_insert  (GRelation   *relation,
  2302.                    ...);
  2303. gint       g_relation_delete  (GRelation   *relation,
  2304.                    gconstpointer  key,
  2305.                    gint        field);
  2306. GTuples*   g_relation_select  (GRelation   *relation,
  2307.                    gconstpointer  key,
  2308.                    gint        field);
  2309. gint       g_relation_count   (GRelation   *relation,
  2310.                    gconstpointer  key,
  2311.                    gint        field);
  2312. gboolean   g_relation_exists  (GRelation   *relation,
  2313.                    ...);
  2314. void       g_relation_print   (GRelation   *relation);
  2315.  
  2316. void       g_tuples_destroy   (GTuples       *tuples);
  2317. gpointer   g_tuples_index     (GTuples       *tuples,
  2318.                    gint        index,
  2319.                    gint        field);
  2320.  
  2321.  
  2322. /* Prime numbers.
  2323.  */
  2324.  
  2325. /* This function returns prime numbers spaced by approximately 1.5-2.0
  2326.  * and is for use in resizing data structures which prefer
  2327.  * prime-valued sizes.    The closest spaced prime function returns the
  2328.  * next largest prime, or the highest it knows about which is about
  2329.  * MAXINT/4.
  2330.  */
  2331. guint       g_spaced_primes_closest (guint num);
  2332.  
  2333.  
  2334. /* GIOChannel
  2335.  */
  2336.  
  2337. typedef struct _GIOFuncs GIOFuncs;
  2338. typedef enum
  2339. {
  2340.   G_IO_ERROR_NONE,
  2341.   G_IO_ERROR_AGAIN,
  2342.   G_IO_ERROR_INVAL,
  2343.   G_IO_ERROR_UNKNOWN
  2344. } GIOError;
  2345. typedef enum
  2346. {
  2347.   G_SEEK_CUR,
  2348.   G_SEEK_SET,
  2349.   G_SEEK_END
  2350. } GSeekType;
  2351. typedef enum
  2352. {
  2353.   G_IO_IN    GLIB_SYSDEF_POLLIN,
  2354.   G_IO_OUT    GLIB_SYSDEF_POLLOUT,
  2355.   G_IO_PRI    GLIB_SYSDEF_POLLPRI,
  2356.   G_IO_ERR    GLIB_SYSDEF_POLLERR,
  2357.   G_IO_HUP    GLIB_SYSDEF_POLLHUP,
  2358.   G_IO_NVAL    GLIB_SYSDEF_POLLNVAL
  2359. } GIOCondition;
  2360.  
  2361. struct _GIOChannel
  2362. {
  2363.   guint channel_flags;
  2364.   guint ref_count;
  2365.   GIOFuncs *funcs;
  2366. };
  2367.  
  2368. typedef gboolean (*GIOFunc) (GIOChannel   *source,
  2369.                  GIOCondition  condition,
  2370.                  gpointer      data);
  2371. struct _GIOFuncs
  2372. {
  2373.   GIOError (*io_read)   (GIOChannel     *channel, 
  2374.                  gchar          *buf, 
  2375.                  guint           count,
  2376.              guint          *bytes_read);
  2377.   GIOError (*io_write)  (GIOChannel     *channel, 
  2378.               gchar          *buf, 
  2379.              guint           count,
  2380.              guint          *bytes_written);
  2381.   GIOError (*io_seek)   (GIOChannel       *channel, 
  2382.               gint            offset, 
  2383.                GSeekType       type);
  2384.   void (*io_close)      (GIOChannel    *channel);
  2385.   guint (*io_add_watch) (GIOChannel     *channel,
  2386.              gint            priority,
  2387.              GIOCondition    condition,
  2388.              GIOFunc         func,
  2389.              gpointer        user_data,
  2390.              GDestroyNotify  notify);
  2391.   void (*io_free)       (GIOChannel    *channel);
  2392. };
  2393.  
  2394. void        g_io_channel_init   (GIOChannel    *channel);
  2395. void        g_io_channel_ref    (GIOChannel    *channel);
  2396. void        g_io_channel_unref  (GIOChannel    *channel);
  2397. GIOError    g_io_channel_read   (GIOChannel    *channel, 
  2398.                      gchar         *buf, 
  2399.                      guint          count,
  2400.                      guint         *bytes_read);
  2401. GIOError  g_io_channel_write    (GIOChannel    *channel, 
  2402.                      gchar         *buf, 
  2403.                      guint          count,
  2404.                      guint         *bytes_written);
  2405. GIOError  g_io_channel_seek     (GIOChannel    *channel,
  2406.                      gint           offset, 
  2407.                      GSeekType      type);
  2408. void      g_io_channel_close    (GIOChannel    *channel);
  2409. guint     g_io_add_watch_full   (GIOChannel    *channel,
  2410.                      gint           priority,
  2411.                      GIOCondition   condition,
  2412.                      GIOFunc        func,
  2413.                      gpointer       user_data,
  2414.                      GDestroyNotify notify);
  2415. guint    g_io_add_watch         (GIOChannel    *channel,
  2416.                      GIOCondition   condition,
  2417.                      GIOFunc        func,
  2418.                      gpointer       user_data);
  2419.  
  2420.  
  2421. /* Main loop
  2422.  */
  2423. typedef struct _GTimeVal    GTimeVal;
  2424. typedef struct _GSourceFuncs    GSourceFuncs;
  2425. typedef struct _GMainLoop    GMainLoop;    /* Opaque */
  2426.  
  2427. struct _GTimeVal
  2428. {
  2429.   glong tv_sec;
  2430.   glong tv_usec;
  2431. };
  2432. struct _GSourceFuncs
  2433. {
  2434.   gboolean (*prepare)  (gpointer  source_data, 
  2435.             GTimeVal *current_time,
  2436.             gint     *timeout,
  2437.             gpointer  user_data);
  2438.   gboolean (*check)    (gpointer  source_data,
  2439.             GTimeVal *current_time,
  2440.             gpointer  user_data);
  2441.   gboolean (*dispatch) (gpointer  source_data, 
  2442.             GTimeVal *dispatch_time,
  2443.             gpointer  user_data);
  2444.   GDestroyNotify destroy;
  2445. };
  2446.  
  2447. /* Standard priorities */
  2448.  
  2449. #define G_PRIORITY_HIGH            -100
  2450. #define G_PRIORITY_DEFAULT          0
  2451. #define G_PRIORITY_HIGH_IDLE        100
  2452. #define G_PRIORITY_DEFAULT_IDLE     200
  2453. #define G_PRIORITY_LOW                300
  2454.  
  2455. typedef gboolean (*GSourceFunc) (gpointer data);
  2456.  
  2457. /* Hooks for adding to the main loop */
  2458. guint    g_source_add                        (gint           priority, 
  2459.                           gboolean       can_recurse,
  2460.                           GSourceFuncs  *funcs,
  2461.                           gpointer       source_data, 
  2462.                           gpointer       user_data,
  2463.                           GDestroyNotify notify);
  2464. gboolean g_source_remove                     (guint          tag);
  2465. gboolean g_source_remove_by_user_data        (gpointer       user_data);
  2466. gboolean g_source_remove_by_source_data      (gpointer       source_data);
  2467. gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
  2468.                           gpointer       user_data);
  2469.  
  2470. void g_get_current_time                (GTimeVal    *result);
  2471.  
  2472. /* Running the main loop */
  2473. GMainLoop*    g_main_new        (gboolean     is_running);
  2474. void        g_main_run        (GMainLoop    *loop);
  2475. void        g_main_quit        (GMainLoop    *loop);
  2476. void        g_main_destroy        (GMainLoop    *loop);
  2477. gboolean    g_main_is_running    (GMainLoop    *loop);
  2478.  
  2479. /* Run a single iteration of the mainloop. If block is FALSE,
  2480.  * will never block
  2481.  */
  2482. gboolean    g_main_iteration    (gboolean    may_block);
  2483.  
  2484. /* See if any events are pending */
  2485. gboolean    g_main_pending        (void);
  2486.  
  2487. /* Idles and timeouts */
  2488. guint        g_timeout_add_full    (gint           priority,
  2489.                      guint          interval, 
  2490.                      GSourceFunc    function,
  2491.                      gpointer       data,
  2492.                      GDestroyNotify notify);
  2493. guint        g_timeout_add        (guint          interval,
  2494.                      GSourceFunc    function,
  2495.                      gpointer       data);
  2496. guint        g_idle_add           (GSourceFunc    function,
  2497.                      gpointer    data);
  2498. guint           g_idle_add_full        (gint       priority,
  2499.                      GSourceFunc    function,
  2500.                      gpointer    data,
  2501.                      GDestroyNotify destroy);
  2502. gboolean    g_idle_remove_by_data    (gpointer    data);
  2503.  
  2504. /* GPollFD
  2505.  *
  2506.  * System-specific IO and main loop calls
  2507.  *
  2508.  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
  2509.  * descriptor as provided by the C runtime) that can be used by
  2510.  * MsgWaitForMultipleObjects. This does *not* include file handles
  2511.  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
  2512.  * WSAEventSelect to signal events when a SOCKET is readable).
  2513.  *
  2514.  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
  2515.  * indicate polling for messages. These message queue GPollFDs should
  2516.  * be added with the g_main_poll_win32_msg_add function.
  2517.  *
  2518.  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
  2519.  * (GTK) programs, as GDK itself wants to read messages and convert them
  2520.  * to GDK events.
  2521.  *
  2522.  * So, unless you really know what you are doing, it's best not to try
  2523.  * to use the main loop polling stuff for your own needs on
  2524.  * Win32. It's really only written for the GIMP's needs so
  2525.  * far.
  2526.  */
  2527.  
  2528. typedef struct _GPollFD GPollFD;
  2529. typedef gint    (*GPollFunc)    (GPollFD *ufds,
  2530.                  guint      nfsd,
  2531.                  gint     timeout);
  2532. struct _GPollFD
  2533. {
  2534.   gint        fd;
  2535.   gushort     events;
  2536.   gushort     revents;
  2537. };
  2538.  
  2539. void        g_main_add_poll          (GPollFD    *fd,
  2540.                       gint        priority);
  2541. void        g_main_remove_poll       (GPollFD    *fd);
  2542. void        g_main_set_poll_func     (GPollFunc   func);
  2543.  
  2544. /* On Unix, IO channels created with this function for any file
  2545.  * descriptor or socket.
  2546.  *
  2547.  * On Win32, use this only for plain files opened with the MSVCRT (the
  2548.  * Microsoft run-time C library) _open(), including file descriptors
  2549.  * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
  2550.  * Actually, don't do even that, this code isn't done yet.
  2551.  *
  2552.  * The term file descriptor as used in the context of Win32 refers to
  2553.  * the emulated Unix-like file descriptors MSVCRT provides.
  2554.  */
  2555. GIOChannel* g_io_channel_unix_new    (int         fd);
  2556. gint        g_io_channel_unix_get_fd (GIOChannel *channel);
  2557.  
  2558. #ifdef NATIVE_WIN32
  2559.  
  2560. GUTILS_C_VAR guint g_pipe_readable_msg;
  2561.  
  2562. #define G_WIN32_MSG_HANDLE 19981206
  2563.  
  2564. /* This is used to add polling for Windows messages. GDK (GTk+) programs
  2565.  * should *not* use this. (In fact, I can't think of any program that
  2566.  * would want to use this, but it's here just for completeness's sake.
  2567.  */
  2568. void        g_main_poll_win32_msg_add(gint        priority,
  2569.                       GPollFD    *fd,
  2570.                       guint       hwnd);
  2571.  
  2572. /* An IO channel for Windows messages for window handle hwnd. */
  2573. GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
  2574.  
  2575. /* An IO channel for an anonymous pipe as returned from the MSVCRT
  2576.  * _pipe(), with no mechanism for the writer to tell the reader when
  2577.  * there is data in the pipe.
  2578.  *
  2579.  * This is not really implemented yet.
  2580.  */
  2581. GIOChannel *g_io_channel_win32_new_pipe (int fd);
  2582.  
  2583. /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
  2584.  * Windows user messages used to signal data in the pipe for the
  2585.  * reader.
  2586.  *
  2587.  * fd is the file descriptor. For the write end, peer is the thread id
  2588.  * of the reader, and peer_fd is his file descriptor for the read end
  2589.  * of the pipe.
  2590.  *
  2591.  * This is used by the GIMP, and works.
  2592.  */
  2593. GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int   fd,
  2594.                               guint peer,
  2595.                               int   peer_fd);
  2596.  
  2597. void        g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
  2598.                              guint       peer,
  2599.                              int         peer_fd);
  2600.  
  2601. void        g_io_channel_win32_pipe_readable (int   fd,
  2602.                           guint offset);
  2603.  
  2604. /* Get the C runtime file descriptor of a channel. */
  2605. gint        g_io_channel_win32_get_fd (GIOChannel *channel);
  2606.  
  2607. /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
  2608.  * actually a SOCKET.
  2609.  */
  2610. GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
  2611.  
  2612. #endif
  2613.  
  2614. /* Windows emulation stubs for common Unix functions
  2615.  */
  2616. #ifdef NATIVE_WIN32
  2617. #  define MAXPATHLEN 1024
  2618. #  ifdef _MSC_VER
  2619. typedef int pid_t;
  2620.  
  2621. /* These POSIXish functions are available in the Microsoft C library
  2622.  * prefixed with underscore (which of course technically speaking is
  2623.  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
  2624.  * prevents Microsoft from practically requiring you to include
  2625.  * <windows.h> every now and then...).
  2626.  *
  2627.  * You still need to include the appropriate headers to get the
  2628.  * prototypes, <io.h> or <direct.h>.
  2629.  *
  2630.  * For some functions, we provide emulators in glib, which are prefixed
  2631.  * with gwin_.
  2632.  */
  2633. #    define getcwd        _getcwd
  2634. #    define getpid        _getpid
  2635. #    define access        _access
  2636. #    define open        _open
  2637. #    define read        _read
  2638. #    define write        _write
  2639. #    define lseek        _lseek
  2640. #    define close        _close
  2641. #    define pipe(phandles)    _pipe (phandles, 4096, _O_BINARY)
  2642. #    define popen        _popen
  2643. #    define pclose        _pclose
  2644. #    define fdopen        _fdopen
  2645. #    define ftruncate(fd, size)    gwin_ftruncate (fd, size)
  2646. #    define opendir        gwin_opendir
  2647. #    define readdir        gwin_readdir
  2648. #    define rewinddir        gwin_rewinddir
  2649. #    define closedir        gwin_closedir
  2650. #    define NAME_MAX 255
  2651. struct DIR
  2652. {
  2653.   gchar    *dir_name;
  2654.   gboolean  just_opened;
  2655.   guint     find_file_handle;
  2656.   gpointer  find_file_data;
  2657. };
  2658. typedef struct DIR DIR;
  2659. struct dirent
  2660. {
  2661.   gchar  d_name[NAME_MAX + 1];
  2662. };
  2663. /* emulation functions */
  2664. extern int    gwin_ftruncate    (gint         f,
  2665.                  guint         size);
  2666. DIR*        gwin_opendir    (const gchar    *dirname);
  2667. struct dirent*    gwin_readdir      (DIR        *dir);
  2668. void        gwin_rewinddir     (DIR        *dir);
  2669. gint        gwin_closedir      (DIR        *dir);
  2670. #  endif /* _MSC_VER */
  2671. #endif     /* NATIVE_WIN32 */
  2672.  
  2673.  
  2674. /* GLib Thread support
  2675.  */
  2676. typedef struct _GMutex        GMutex;
  2677. typedef struct _GCond        GCond;
  2678. typedef struct _GPrivate    GPrivate;
  2679. typedef struct _GStaticPrivate    GStaticPrivate;
  2680. typedef struct _GThreadFunctions GThreadFunctions;
  2681. struct _GThreadFunctions
  2682. {
  2683.   GMutex*  (*mutex_new)       (void);
  2684.   void     (*mutex_lock)      (GMutex        *mutex);
  2685.   gboolean (*mutex_trylock)   (GMutex        *mutex);
  2686.   void     (*mutex_unlock)    (GMutex        *mutex);
  2687.   void     (*mutex_free)      (GMutex        *mutex);
  2688.   GCond*   (*cond_new)        (void);
  2689.   void     (*cond_signal)     (GCond        *cond);
  2690.   void     (*cond_broadcast)  (GCond        *cond);
  2691.   void     (*cond_wait)       (GCond        *cond,
  2692.                    GMutex        *mutex);
  2693.   gboolean (*cond_timed_wait) (GCond        *cond,
  2694.                    GMutex        *mutex, 
  2695.                    GTimeVal     *end_time);
  2696.   void      (*cond_free)      (GCond        *cond);
  2697.   GPrivate* (*private_new)    (GDestroyNotify     destructor);
  2698.   gpointer  (*private_get)    (GPrivate        *private_key);
  2699.   void      (*private_set)    (GPrivate        *private_key,
  2700.                    gpointer         data);
  2701. };
  2702.  
  2703. GUTILS_C_VAR GThreadFunctions    g_thread_functions_for_glib_use;
  2704. GUTILS_C_VAR gboolean        g_thread_use_default_impl;
  2705. GUTILS_C_VAR gboolean        g_threads_got_initialized;
  2706.  
  2707. /* initializes the mutex/cond/private implementation for glib, might
  2708.  * only be called once, and must not be called directly or indirectly
  2709.  * from another glib-function, e.g. as a callback.
  2710.  */
  2711. void   g_thread_init   (GThreadFunctions       *vtable);
  2712.  
  2713. /* internal function for fallback static mutex implementation */
  2714. GMutex*    g_static_mutex_get_mutex_impl    (GMutex    **mutex);
  2715.  
  2716. /* shorthands for conditional and unconditional function calls */
  2717. #define G_THREAD_UF(name, arglist) \
  2718.     (*g_thread_functions_for_glib_use . name) arglist
  2719. #define G_THREAD_CF(name, fail, arg) \
  2720.     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
  2721. /* keep in mind, all those mutexes and static mutexes are not 
  2722.  * recursive in general, don't rely on that
  2723.  */
  2724. #define    g_thread_supported()    (g_threads_got_initialized)
  2725. #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
  2726. #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
  2727. #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
  2728. #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
  2729. #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
  2730. #define g_cond_new()             G_THREAD_UF (cond_new,       ())
  2731. #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
  2732. #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
  2733. #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
  2734.                                                                         mutex))
  2735. #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
  2736. #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
  2737.                                                               TRUE, \
  2738.                                                               (cond, mutex, \
  2739.                                    abs_time))
  2740. #define g_private_new(destructor)      G_THREAD_UF (private_new, (destructor))
  2741. #define g_private_get(private_key)      G_THREAD_CF (private_get, \
  2742.                                                        ((gpointer)private_key), \
  2743.                                                        (private_key))
  2744. #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
  2745.                                                        (void) (private_key = \
  2746.                                                         (GPrivate*) (value)), \
  2747.                                                        (private_key, value))
  2748. /* GStaticMutexes can be statically initialized with the value
  2749.  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
  2750.  * much easier, than having to explicitly allocate the mutex before
  2751.  * use
  2752.  */
  2753. #define g_static_mutex_lock(mutex) \
  2754.     g_mutex_lock (g_static_mutex_get_mutex (mutex))
  2755. #define g_static_mutex_trylock(mutex) \
  2756.     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
  2757. #define g_static_mutex_unlock(mutex) \
  2758.     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
  2759. struct _GStaticPrivate
  2760. {
  2761.   guint index;
  2762. };
  2763. #define G_STATIC_PRIVATE_INIT { 0 }
  2764. gpointer g_static_private_get (GStaticPrivate    *private_key);
  2765. void     g_static_private_set (GStaticPrivate    *private_key, 
  2766.                    gpointer             data,
  2767.                    GDestroyNotify    notify);
  2768.  
  2769. /* these are some convenience macros that expand to nothing if GLib
  2770.  * was configured with --disable-threads. for using StaticMutexes,
  2771.  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
  2772.  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
  2773.  * declare such an globally defined lock. name is a unique identifier
  2774.  * for the protected varibale or code portion. locking, testing and
  2775.  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
  2776.  * G_TRYLOCK() respectively.  
  2777.  */
  2778. extern void glib_dummy_decl (void);
  2779. #define G_LOCK_NAME(name)        (g__ ## name ## _lock)
  2780. #ifdef    G_THREADS_ENABLED
  2781. #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
  2782. #  define G_LOCK_DEFINE(name)        \
  2783.     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
  2784. #  define G_LOCK_EXTERN(name)        extern GStaticMutex G_LOCK_NAME (name)
  2785.  
  2786. #  ifdef G_DEBUG_LOCKS
  2787. #    define G_LOCK(name)        G_STMT_START{          \
  2788.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2789.            "file %s: line %d (%s): locking: %s ",              \
  2790.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2791.                #name);                                            \
  2792.         g_static_mutex_lock (&G_LOCK_NAME (name));                \
  2793.      }G_STMT_END
  2794. #    define G_UNLOCK(name)        G_STMT_START{          \
  2795.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2796.            "file %s: line %d (%s): unlocking: %s ",              \
  2797.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2798.                #name);                                            \
  2799.        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
  2800.      }G_STMT_END
  2801. #    define G_TRYLOCK(name)        G_STMT_START{          \
  2802.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2803.            "file %s: line %d (%s): try locking: %s ",         \
  2804.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2805.                #name);                                            \
  2806.      }G_STMT_END,    g_static_mutex_trylock (&G_LOCK_NAME (name))
  2807. #  else     /* !G_DEBUG_LOCKS */
  2808. #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name)) 
  2809. #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
  2810. #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
  2811. #  endif /* !G_DEBUG_LOCKS */
  2812. #else    /* !G_THREADS_ENABLED */
  2813. #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
  2814. #  define G_LOCK_DEFINE(name)        extern void glib_dummy_decl (void)
  2815. #  define G_LOCK_EXTERN(name)        extern void glib_dummy_decl (void)
  2816. #  define G_LOCK(name)
  2817. #  define G_UNLOCK(name)
  2818. #  define G_TRYLOCK(name)        (FALSE)
  2819. #endif    /* !G_THREADS_ENABLED */
  2820.  
  2821. #ifdef __cplusplus
  2822. }
  2823. #endif /* __cplusplus */
  2824.  
  2825.  
  2826. #endif /* __G_LIB_H__ */
  2827.